When to use #define and When to use const?

P

Peng Yu

I'm not very clear about when to use #define and when to use const?

#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
 
E

E. Robert Tisdale

Peng said:
I'm not very clear about when to use #define and when to use const?

#define number 4
const int number = 4;

If I just want to define a global constant,
which way of the above is better?

const int number = 4;

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;
}
 
G

Gordon Burditt

I'm not very clear about when to use #define and when to use const?
#define number 4
const int number = 4;

If I just want to define a global constant, which way of the above is
better?

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
 
C

CBFalconer

E. Robert Tisdale said:
const int number = 4;

is better.

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.
 
C

Chris Torek

... In C the declaration "const int number = 4;" simply creates a
variable that is expected to be read-only (but which can be altered).

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.)
 
K

Keith Thompson

E. Robert Tisdale said:
Peng said:
I'm not very clear about when to use #define and when to use const?
#define number 4
const int number = 4;
If I just want to define a global constant, which way of the above
is better?

const int number = 4;

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

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".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top