What is the standard to declare constant variables in ANSI C?
const
in C is very different to const
in C++.
In C it means that the object won't be modified through that identifier:
int a = 42;
const int *b = &a;
*b = 12; /* invalid, the contents of `b` are const */
a = 12; /* ok, even though *b changed */
Also, unlike C++, const objects cannot be used, for instance, in switch labels:
const int k = 0;
switch (x) {
case k: break; /* invalid use of const object */
}
So ... it really depends on what you need.
Your options are
#define
: really const but uses the preprocessorconst
: not really constenum
: limited toint
larger example
#define CONST 42
const int konst = 42;
enum /*unnamed*/ { fixed = 42 };
printf("%d %d %d\n", CONST, konst, fixed);
/* &CONST makes no sense */
&konst; /* can be used */
/* &fixed makes no sense */