C में Constant Expression: कहां ज़रूरी है और क्यों
C में constant expression compile time पर evaluate होता है. यह array sizes, case labels, bit-fields और #if में ज़रूरी है. पूरे examples और rules अंदर देखें.
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
C में constant expression क्या है?
Constant expression वह expression है जिसकी value compiler compile time पर पूरी तरह compute कर सकता है, जैसे literals, literals पर arithmetic, sizeof, character constants और enum constants.
क्या C में const variable constant expression है?
नहीं. C में const variable सिर्फ read-only runtime object है, इसलिए case labels जैसी जगहों पर use नहीं हो सकता जहां constant expression चाहिए. C++ में behaviour अलग है.
Constant expressions कहां ज़रूरी हैं?
Constant expressions non-VLA array sizes, switch case labels, structure bit-field widths और #if preprocessor conditions में ज़रूरी हैं.