Which one is better to use among the below statements in C?
static const int var = 5;
or
#define var 5
o
#define var 5
will cause you trouble if you have things like mystruct.var
.
For example,
struct mystruct {
int var;
};
#define var 5
int main() {
struct mystruct foo;
foo.var = 1;
return 0;
}
The preprocessor will replace it and the code won't compile. For this reason, traditional coding style suggest all constant #define
s uses capital letters to avoid conflict.