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 readable —
PIis clearer than3.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.
#define CONSTANT_NAME value // Note: No semicolon! No data type! No = sign!
#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; }
By convention, constants defined with #define are written in ALL_CAPS with underscores. This makes them easily distinguishable from variables.
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.
#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; }
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.
#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; }
#define vs const
| Feature | #define | const |
|---|---|---|
| Processing | Preprocessor (before compile) | Compiler (during compile) |
| Data type | No type — just text substitution | Has a specific data type |
| Memory | No memory allocated | Memory allocated (like a variable) |
| Scope | Global (from definition to end of file) | Follows block scope rules |
| Debugging | Harder (no type info) | Easier (type checking by compiler) |
| Pointer | Cannot get address | Can get address |
| Best for | Simple values, macros | Typed constants, local constants |
Summary
- Constants = values that don't change during program execution
#define NAME value— preprocessor text substitution, no type, no semicolonconst type NAME = value;— typed constant, compiler-checkedenum— set of related named integer constants, starts at 0 by default- Use ALL_CAPS naming for constants by convention
- Prefer
constover#definefor type safety (modern C practice)
Constant क्या है?
Constant वो value है जो program execution के दौरान बदल नहीं सकती। Variable के विपरीत, constant define होने के बाद उसकी value हमेशा fixed रहती है।
- Readable —
PI,3.14159से ज़्यादा clear है - Maintain करने में आसान — एक जगह value बदलो, पूरा program update
- Safe — constant modify करने पर compiler error देता है
#define – Preprocessor Macro
#define directive symbolic constant बनाता है। Preprocessor compilation से पहले हर occurrence को value से replace करता है।
#define CONSTANT_NAME value // ध्यान दें: कोई semicolon नहीं! कोई data type नहीं! कोई = नहीं!
#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; }
Constants को ALL_CAPS में लिखते हैं (underscore के साथ)। इससे वो variables से easily अलग दिखते हैं।
const Keyword
const keyword एक variable को read-only बना देता है। #define के विपरीत, इसका data type होता है।
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 बनता है।
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; }
#define vs const में अंतर
| Feature | #define | const |
|---|---|---|
| Processing | Preprocessor द्वारा | Compiler द्वारा |
| Data type | कोई type नहीं | Specific data type होता है |
| Memory | Memory allocate नहीं होती | Memory allocate होती है |
| Scope | Global (file के अंत तक) | Block scope rules follow करता है |
| Best for | Simple values, macros | Typed constants |
सारांश
- Constants = वो values जो program में बदलती नहीं
#define NAME value— preprocessor text substitution, no type, no semicolonconst type NAME = value;— typed constant, compiler द्वारा checkenum— related named integer constants का set, default 0 से शुरू- Convention: constants को ALL_CAPS में लिखें
- Type safety के लिए
#defineकी बजायconstprefer करें