🟢 Beginner  ·  Lesson 07

Constants – #define, const, enum

Constants – #define, const

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)

Constant क्या है?

Constant वो value है जो program execution के दौरान बदल नहीं सकती। Variable के विपरीत, constant define होने के बाद उसकी value हमेशा fixed रहती है।

  • ReadablePI, 3.14159 से ज़्यादा clear है
  • Maintain करने में आसान — एक जगह value बदलो, पूरा program update
  • Safe — constant modify करने पर compiler error देता है

#define – Preprocessor Macro

#define directive symbolic constant बनाता है। Preprocessor compilation से पहले हर occurrence को value से replace करता है।

Syntax
#define CONSTANT_NAME value
// ध्यान दें: कोई semicolon नहीं! कोई data type नहीं! कोई = नहीं!
C Language
#include <stdio.h>

#define PI       3.14159
#define MAX_SIZE 100
#define SCHOOL   "Alpine Public School"

int main() {
    float r = 7.0f;
    printf("Kshetrafal = %.2f\n", PI * r * r);
    printf("School: %s\n", SCHOOL);
    printf("Max size: %d\n", MAX_SIZE);
    return 0;
}
Kshetrafal = 153.94 School: Alpine Public School Max size: 100
💡 Naming Convention

Constants को ALL_CAPS में लिखते हैं (underscore के साथ)। इससे वो variables से easily अलग दिखते हैं।

const Keyword

const keyword एक variable को read-only बना देता है। #define के विपरीत, इसका data type होता है।

C Language
const float PI = 3.14159f;
const int   MAX = 100;
const char  GRADE = 'A';

printf("PI = %.5f\n", PI);

// PI = 3.14;  // ERROR! const को modify नहीं कर सकते

enum – Named Constants

enum related named integer constants का set बनाता है। Magic numbers की जगह meaningful names use करने से code readable बनता है।

C Language
enum Din { SOM, MANGAL, BUDH, BRIHASPATI, SHUKRA, SHANIV, RAVI };
// SOM=0, MANGAL=1, BUDH=2, SHUKRA=4, ...

enum Traffic { LAL, PEELA, HARA };

int main() {
    enum Din aaj = SHUKRA;
    printf("Shukravar = %d\n", aaj);     // 4

    enum Traffic signal = HARA;
    if (signal == HARA) printf("Jao!\n");
    return 0;
}
Shukravar = 4 Jao!

#define vs const में अंतर

Feature#defineconst
ProcessingPreprocessor द्वाराCompiler द्वारा
Data typeकोई type नहींSpecific data type होता है
MemoryMemory allocate नहीं होतीMemory allocate होती है
ScopeGlobal (file के अंत तक)Block scope rules follow करता है
Best forSimple values, macrosTyped constants

सारांश

  • Constants = वो values जो program में बदलती नहीं
  • #define NAME value — preprocessor text substitution, no type, no semicolon
  • const type NAME = value; — typed constant, compiler द्वारा check
  • enum — related named integer constants का set, default 0 से शुरू
  • Convention: constants को ALL_CAPS में लिखें
  • Type safety के लिए #define की बजाय const prefer करें
← Back to C Tutorial