🟢 Beginner  ·  Lesson 07

Constants – #define, const, enum

What is a Constant?

A constant is a value that cannot be changed during program execution. Unlike variables, once a constant is defined, its value remains fixed throughout the program. Constants make code:

  • More readablePI is clearer than 3.14159
  • Easier to maintain — change the value in one place only
  • Safer — compiler error if you try to modify a constant

C provides three ways to define constants: #define, const, and enum.

#define – Preprocessor Macro

The #define directive creates a symbolic constant (also called a macro). The preprocessor replaces every occurrence of the macro with its value before compilation — it's a simple text substitution.

Syntax
#define CONSTANT_NAME value
// Note: No semicolon! No data type! No = sign!
C Language – #define Examples
#include <stdio.h>

#define PI         3.14159
#define MAX_SIZE   100
#define SCHOOL     "Alpine Public School"
#define TRUE       1
#define FALSE      0
#define SQUARE(x)  ((x) * (x))   // Macro with parameter

int main() {
    float radius = 7.0f;
    float area = PI * radius * radius;
    printf("Area of circle = %.2f\n", area);
    printf("School: %s\n", SCHOOL);
    printf("Square of 5 = %d\n", SQUARE(5));
    printf("Max array size = %d\n", MAX_SIZE);
    return 0;
}
Area of circle = 153.94 School: Alpine Public School Square of 5 = 25 Max array size = 100
💡 Naming Convention

By convention, constants defined with #define are written in ALL_CAPS with underscores. This makes them easily distinguishable from variables.

⚠️ #define Pitfalls

Always use parentheses in macro expressions: #define SQUARE(x) ((x)*(x)) not #define SQUARE(x) x*x. Without parentheses, SQUARE(2+3) becomes 2+3*2+3 = 11 instead of 25!

const Keyword

The const keyword makes a variable read-only. Unlike #define, it has a data type and follows all variable scoping rules.

C Language – const Examples
#include <stdio.h>

int main() {
    const float PI = 3.14159f;
    const int   MAX = 100;
    const char  GRADE = 'A';

    printf("PI    = %.5f\n", PI);
    printf("MAX   = %d\n", MAX);
    printf("GRADE = %c\n", GRADE);

    // PI = 3.14;   // ERROR! Cannot modify const
    // MAX = 200;   // ERROR!

    return 0;
}
PI = 3.14159 MAX = 100 GRADE = A

enum – Named Integer Constants

enum (enumeration) lets you create a set of related named integer constants. It improves code readability by replacing magic numbers with meaningful names.

C Language – enum Examples
#include <stdio.h>

// By default: MON=0, TUE=1, WED=2, THU=3, FRI=4, SAT=5, SUN=6
enum Day { MON, TUE, WED, THU, FRI, SAT, SUN };

// Custom values
enum Status { PENDING = 1, ACTIVE = 2, INACTIVE = 3, DELETED = 99 };

// Traffic light
enum Light { RED, YELLOW, GREEN };

int main() {
    enum Day today = FRI;
    printf("Friday value = %d\n", today);   // 4
    printf("Sunday value = %d\n", SUN);     // 6

    enum Light signal = GREEN;
    if (signal == GREEN) {
        printf("Go! Signal is GREEN\n");
    }

    enum Status userStatus = ACTIVE;
    printf("Status code = %d\n", userStatus); // 2
    return 0;
}
Friday value = 4 Sunday value = 6 Go! Signal is GREEN Status code = 2

#define vs const

Feature#defineconst
ProcessingPreprocessor (before compile)Compiler (during compile)
Data typeNo type — just text substitutionHas a specific data type
MemoryNo memory allocatedMemory allocated (like a variable)
ScopeGlobal (from definition to end of file)Follows block scope rules
DebuggingHarder (no type info)Easier (type checking by compiler)
PointerCannot get addressCan get address
Best forSimple values, macrosTyped constants, local constants

Summary

  • Constants = values that don't change during program execution
  • #define NAME value — preprocessor text substitution, no type, no semicolon
  • const type NAME = value; — typed constant, compiler-checked
  • enum — set of related named integer constants, starts at 0 by default
  • Use ALL_CAPS naming for constants by convention
  • Prefer const over #define for type safety (modern C practice)

Frequently Asked Questions

What is a constant in C?
A constant is a value that cannot be changed while the program runs. Once set, any attempt to modify it is an error. Constants make code safer and clearer by protecting values such as the number of days in a week or the value of pi from accidental change.
What is the difference between #define and const in C?
#define creates a preprocessor macro that is simply text-substituted before compilation and has no type. const creates a real typed variable that cannot be modified, so the compiler checks its type and it can be inspected while debugging. const is generally preferred for typed constants.
How do you declare a constant in C?
Two common ways: use #define PI 3.14159 as a preprocessor macro, or declare a variable with the const keyword such as const int DAYS = 7;. Both give you a value that should not change during the program.
What is a symbolic constant in C?
A symbolic constant is a name given to a fixed value, usually with #define, so you can write a meaningful name instead of a raw number throughout your code. Changing the value then means editing just one line, which reduces errors.
Why use constants instead of literal values?
Using a named constant makes code easier to read and maintain: a name like MAX_USERS explains its meaning, and if the value needs to change you update it in one place. It also prevents accidental changes to values that must stay fixed.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.