Constant Expression in C: Where It Is Required and Why
A constant expression in C is evaluated at compile time. It is required in array sizes, case labels, bit-fields and #if. Full examples and rules inside.
What is a constant expression?
10, 5 + 3, 2 * 60 * 60, 'A', sizeof(int), enum constants and #define values that expand to these.10 + 20 /* constant expression: 30 at compile time */
MAX * 2 /* constant if MAX is a #define or enum */
x + 5 /* NOT constant: x is a runtime variable */
The C surprise: const is NOT a constant expression
const variable is not a constant expression. So this fails:
const int SIZE = 10;
switch (n) {
case SIZE: /* ERROR in C: case label needs constant expression */
break;
}
Use #define SIZE 10 or enum { SIZE = 10 }; instead.4 places where a constant expression is required
1. Array sizes (non-VLA):
#define N 5
int arr[N]; /* OK: N is a constant expression */
int arr2[10 + 5]; /* OK: computed at compile time */
2. Case labels in switch:
enum { ADMIN = 1, TEACHER = 2 };
switch (role) {
case ADMIN: printf("Admin panel\n"); break;
case TEACHER: printf("Teacher panel\n"); break;
}
3. Bit-field widths in structures:
struct Flags {
unsigned int active : 1; /* width must be constant expression */
unsigned int level : 3;
};
4. Preprocessor conditions (#if):
#define VERSION 3
#if VERSION >= 2
/* code included only when condition is true at preprocess time */
#endif
Constant vs runtime expression: quick test
| Expression | Constant expression? | Why |
|---|---|---|
50 * 2 | ✅ Yes | Pure literals |
sizeof(double) | ✅ Yes | Known at compile time |
'A' + 1 | ✅ Yes | Character constant is an int value |
const int c = 5; ... c*2 | ❌ No (in C) | const variable is not constant expr |
x + 1 | ❌ No | x known only at runtime |
getMax() | ❌ No | Function runs at runtime |
Constant expression क्या है?
10, 5 + 3, 2 * 60 * 60, 'A', sizeof(int), enum constants और ऐसे #define values.10 + 20 /* constant expression: compile time पर 30 */
MAX * 2 /* constant अगर MAX #define या enum है */
x + 5 /* NOT constant: x runtime variable है */
C का surprise: const constant expression नहीं है
const variable constant expression नहीं होता. इसलिए यह fail होता है:
const int SIZE = 10;
switch (n) {
case SIZE: /* C में ERROR: case label को constant expression चाहिए */
break;
}
इसकी जगह #define SIZE 10 या enum { SIZE = 10 }; use करें.4 जगहें जहां constant expression ज़रूरी है
1. Array sizes (non-VLA):
#define N 5
int arr[N]; /* OK: N constant expression है */
int arr2[10 + 5]; /* OK: compile time पर computed */
2. Switch के case labels:
enum { ADMIN = 1, TEACHER = 2 };
switch (role) {
case ADMIN: printf("Admin panel\n"); break;
case TEACHER: printf("Teacher panel\n"); break;
}
3. Structures में bit-field widths:
struct Flags {
unsigned int active : 1; /* width constant expression होनी चाहिए */
unsigned int level : 3;
};
4. Preprocessor conditions (#if):
#define VERSION 3
#if VERSION >= 2
/* code तभी include होता है जब condition preprocess time पर true हो */
#endif
Constant vs runtime expression: quick test
| Expression | Constant expression? | क्यों |
|---|---|---|
50 * 2 | ✅ हां | Pure literals |
sizeof(double) | ✅ हां | Compile time पर known |
'A' + 1 | ✅ हां | Character constant एक int value है |
const int c = 5; ... c*2 | ❌ नहीं (C में) | const variable constant expr नहीं है |
x + 1 | ❌ नहीं | x सिर्फ runtime पर known |
getMax() | ❌ नहीं | Function runtime पर चलता है |
Frequently Asked Questions
What is a constant expression in C?
A constant expression is an expression whose value the compiler can fully compute at compile time, such as literals, arithmetic on literals, sizeof, character constants and enum constants.
Is a const variable a constant expression in C?
No. In C, a const variable is only a read-only runtime object, so it cannot be used where a constant expression is required, like case labels. C++ behaves differently.
Where are constant expressions required?
Constant expressions are required in non-VLA array sizes, switch case labels, structure bit-field widths and #if preprocessor conditions.