3 ms·
Also, `const` values in C are not really evaluated at compile time, because they have by default external linkage so they are not really "constant", they are mo
by qalmakka 4y ago
Also, `const` values in C are not really evaluated at compile time, because they have by default external linkage so they are not really "constant", they are more like "readonly" or 'let' in Rust. They are always loaded from memory, and that's why #define is still king in C.
Doing
const int X = 33;
// ...
void something() {
int arr[X] = ...
}
works in both C and C++, but with a catch: in C++, X is evaluated at compile time (it has internal linkage), while in C this works only after C99 because it's actually a VLA.
- uecker 4y agoAlthough many compilers allow const int X = 33; enum { Y = X };
- qalmakka 4y agoThat's a GNU extension that everyone relies so much on it's basically akin to a standard. You get a warning from Clang (or an error from GCC) if you ask for a more "standard" interpretation of the source: $ clang -o cs cs.c -Wall -std=c11 -pedantic cs.c:6:6: warning: expression is not an integer constant expression; folding it to a constant is a GNU extension [-Wgnu-folding-constant] A = X, $ gcc -o cs cs.c -Wall -std=c11 cs.c:6:9: error: enumerator value for ‘A’ is not an integer constant 6 | A = X, | ^ In general, that's illegal ISO C and should always be rejected, but as you see that's not usually the case.