Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

When to use #define and When to use const?

58 views
Skip to first unread message

Peng Yu

unread,
Dec 6, 2004, 10:12:49 PM12/6/04
to
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. Robert Tisdale

unread,
Dec 6, 2004, 10:48:37 PM12/6/04
to
Peng Yu wrote:

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

Gordon Burditt

unread,
Dec 6, 2004, 11:03:52 PM12/6/04
to

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

CBFalconer

unread,
Dec 7, 2004, 1:17:27 AM12/7/04
to
"E. Robert Tisdale" wrote:
> Peng Yu wrote:
>
>> 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.

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!


Chris Torek

unread,
Dec 7, 2004, 2:22:25 AM12/7/04
to
In article <41B54753...@yahoo.com>
CBFalconer <cbfal...@worldnet.att.net> wrote:
>... 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.)
--
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.

Keith Thompson

unread,
Dec 7, 2004, 7:15:44 PM12/7/04
to
CBFalconer <cbfal...@yahoo.com> writes:
[...]

> Never accept advice from ERT. It is akin to quoting Schmidt.

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.

Keith Thompson

unread,
Dec 7, 2004, 7:20:04 PM12/7/04
to

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

CBFalconer

unread,
Dec 8, 2004, 12:08:04 AM12/8/04
to
Keith Thompson wrote:
> CBFalconer <cbfal...@yahoo.com> writes:
> [...]
> > Never accept advice from ERT. It is akin to quoting Schmidt.
>
> I think you mean Schildt.

Thanks for the correction. Apologies to all the worlds Schmidts.

0 new messages