#define number 4
const int number = 4;
If I just want to define a global constant, which way of the above is
better?
Thanks,
Peng
is better.
> cat main.c
#include <stdio.h>
const size_t n = 128;
int main(int argc, char* argv[]) {
int array[n];
for (size_t j = 0; j < n; ++j)
array[j] = j;
return 0;
}
> gcc -Wall -std=c99 -pedantic -o main main.c
If you want a constant expression (e.g. suitable for use with 'case'
or C89 array dimensions), use the #define. If you use the const
int number = 4; declaration, number is *NOT* a constant expression.
The declaration:
ant const int number = 4;
is no longer valid since someone fooling around with undefined
behavior from fflush(stdin) removed it from the standard retroactively :-)
Gordon L. Burditt
This is misinformation. In C the declaration "const int number =
4;" simply creates a variable that is expected to be read-only (but
which can be altered). The way to get a constant usable where
constant expressions are needed (such as the size of an array) is
with a #define.
Never accept advice from ERT. It is akin to quoting Schmidt.
--
Chuck F (cbfal...@yahoo.com) (cbfal...@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Well, more precisely, it *may* be alterable, but if you try to do so,
the effect is undefined. In any case, it is indeed still a variable,
even if it never varies.
>The way to get a constant usable where constant expressions are
>needed (such as the size of an array) is with a #define.
This is the most general method, and the one most C programmers use.
For the specific case of "int"-valued constants, you can (mis)use
enum:
enum { number = 4 };
makes "number" an integer constant, of type "int" and value 4. This
can be used in those places that require constants, such as in
sizing arrays -- even those outside a function, or any in C89 --
or in "case" labels:
switch (func()) {
case 1:
... code for "case 1" ...
break;
case number:
... code for "case 4" ...
break;
default:
... code for other cases ...
break;
}
A "const int" is not suitable for any of those three:
% cat t.c
const int number = 4;
char foo[number];
% cc -std=c89 -pedantic -Wall -W -O -c t.c
t.c:2: warning: ISO C89 forbids variable-size array `foo'
t.c:2: variable-size type declared outside of any function
% cc -std=c99 -pedantic -Wall -W -O -c t.c
t.c:2: variable-size type declared outside of any function
% cat t2.c
enum { number = 4 };
char foo[number];
% cc -std=c89 -pedantic -Wall -W -O -c t2.c
% cc -std=c99 -pedantic -Wall -W -O -c t2.c
The "enum" method works; the "const" method does not. (I prefer
the "#define" over the "enum" myself, and "enum" does not allow
defining floating-point constants, or constants of unsigned, long,
or -- in C99 -- long long types.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
I think you mean Schildt.
--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
That is of course illegal in C90.
In C99, if I'm not mistaken, the array is actually a VLA (Variable
Length Array), something not supported in C90. Even though n is
declared "const", it's not a "constant expression".
Thanks for the correction. Apologies to all the worlds Schmidts.