Difference Between const and #define in C (Comparison Table)
const is a typed, scoped read-only variable while #define is a textual macro replaced before compilation. Full comparison table with 6 key differences.
const vs #define: the core idea
One-line answer:
const creates a real, typed, read-only variable checked by the compiler, while #define is a plain text replacement done by the preprocessor before compilation even starts.const int MAX = 100; /* compiler: typed read-only variable */
#define MAX 100 /* preprocessor: text replacement */
Comparison table: 6 key differences
| Point | const | #define |
|---|---|---|
| Handled by | Compiler | Preprocessor (before compilation) |
| Data type | Yes — type checking works | No type — just text |
| Scope | Follows block/function/file scope | Active from #define line till end of file (or #undef) |
| Memory | Usually occupies memory, has an address | No memory, no address |
| Debugger visibility | Visible with its name | Invisible — already replaced by value |
| Usable in case labels / #if | No (in C) | Yes |
Program showing the difference
#include <stdio.h>
#define TAX 18 /* text replacement */
int main() {
const int limit = 500; /* typed variable */
printf("Address of limit: %p\n", (void*)&limit); /* OK */
/* printf("%p", &TAX); */ /* ERROR: TAX has no address */
switch (18) {
case TAX: printf("Tax slab matched\n"); break; /* OK */
/* case limit: */ /* ERROR: not a constant expression in C */
}
return 0;
}
Address of limit: 0x7ffd2c...
Tax slab matched
The classic #define trap
#define SQUARE(x) x * x
int main() {
int r = SQUARE(2 + 3); /* expands to: 2 + 3 * 2 + 3 = 11, not 25! */
return 0;
}
Because #define is blind text replacement,
SQUARE(2 + 3) becomes 2 + 3 * 2 + 3 = 11. const values never suffer from this because they are real evaluated values, not text. (Fix for macros: #define SQUARE(x) ((x)*(x)).)When to use which?
- Use const for typed values:
const double RATE = 8.5;— you get type checking, scoping and debugger support. - Use #define when you need a value in
caselabels, array sizes on very old compilers, or conditional compilation like#if VERSION > 2. - In modern C, the general rule taught in interviews is: "Prefer const over #define for constants; use #define only where const cannot work."
const vs #define: core idea
One-line answer:
const एक real, typed, read-only variable बनाता है जिसे compiler check करता है, जबकि #define plain text replacement है जो compilation शुरू होने से पहले preprocessor करता है.const int MAX = 100; /* compiler: typed read-only variable */
#define MAX 100 /* preprocessor: text replacement */
Comparison table: 6 मुख्य अंतर
| Point | const | #define |
|---|---|---|
| Handle कौन करता है | Compiler | Preprocessor (compilation से पहले) |
| Data type | हां — type checking काम करती है | कोई type नहीं — सिर्फ text |
| Scope | Block/function/file scope follow करता है | #define line से file के अंत तक active (या #undef तक) |
| Memory | आमतौर पर memory लेता है, address होता है | न memory, न address |
| Debugger में दिखना | Name के साथ दिखता है | नहीं दिखता — पहले ही value से replace हो चुका |
| case labels / #if में use | नहीं (C में) | हां |
अंतर दिखाने वाला program
#include <stdio.h>
#define TAX 18 /* text replacement */
int main() {
const int limit = 500; /* typed variable */
printf("Address of limit: %p\n", (void*)&limit); /* OK */
/* printf("%p", &TAX); */ /* ERROR: TAX का कोई address नहीं */
switch (18) {
case TAX: printf("Tax slab matched\n"); break; /* OK */
/* case limit: */ /* ERROR: C में constant expression नहीं */
}
return 0;
}
Address of limit: 0x7ffd2c...
Tax slab matched
#define का classic trap
#define SQUARE(x) x * x
int main() {
int r = SQUARE(2 + 3); /* बनता है: 2 + 3 * 2 + 3 = 11, 25 नहीं! */
return 0;
}
#define blind text replacement है, इसलिए
SQUARE(2 + 3) बन जाता है 2 + 3 * 2 + 3 = 11. const values के साथ यह problem कभी नहीं आती क्योंकि वे real evaluated values हैं, text नहीं. (Macro का fix: #define SQUARE(x) ((x)*(x)).)कब कौन-सा use करें?
- const use करें typed values के लिए:
const double RATE = 8.5;— type checking, scoping और debugger support मिलता है. - #define use करें जब value
caselabels, बहुत पुराने compilers पर array sizes, या#if VERSION > 2जैसी conditional compilation में चाहिए. - Modern C में interviews में यही rule सिखाया जाता है: "Constants के लिए const prefer करें; #define सिर्फ वहां use करें जहां const काम नहीं कर सकता."
Frequently Asked Questions
What is the main difference between const and #define in C?
const is a typed read-only variable processed by the compiler with scope and type checking, while #define is untyped text replacement done by the preprocessor before compilation.
Does #define occupy memory?
No. #define does not create a variable, so it takes no memory and has no address; the value is pasted directly into the code.
Can const be used in a switch case label in C?
No. In C a const variable is not a constant expression, so case labels need #define macros, enum constants or literals.