Which one is better to use among the below statements in C?
static const int var = 5;
or
#define var 5
o
In C #define
is much more popular. You can use those values for declaring array sizes for example:
#define MAXLEN 5
void foo(void) {
int bar[MAXLEN];
}
ANSI C doesn't allow you to use static const
s in this context as far as I know. In C++ you should avoid macros in these cases. You can write
const int maxlen = 5;
void foo() {
int bar[maxlen];
}
and even leave out static
because internal linkage is implied by const
already [in C++ only].