typedef vs #define

Norbert Schlenker nfs at notecnirp.Princeton.EDU
Tue Feb 27 00:32:33 AEST 1990


In article <752 at s5.Morgan.COM> amull at Morgan.COM (Andrew P. Mullhaupt) writes:
>In article <8430 at cbnewsh.ATT.COM>, em at cbnewsh.ATT.COM (edward.man) writes:
>> 
>>	The question is:
>> 
>> Consider the following two C statements:
>> 	typedef short	FLAGS
>> 	#define FLAGS	short
>> 
>> If I had two identical pieces of code, one used the "typedef" and
>> ther other "#define" as defined above, would there be any difference
>> in the compiled code? Does the C compiler handle the two differently?
>
>If your code compiles, it shouldn't result in different object code...

But beware.  If we change the question only slightly, to:

Consider the following two C statements:
 	typedef short * FLAGS
 	#define FLAGS short *
 
If I had two identical pieces of code, one used the "typedef" and
ther other "#define" as defined above, would there be any difference
in the compiled code? Does the C compiler handle the two differently?

Now there's a considerable difference between the two.  Imagine the
declaration:

FLAGS x, y;

The typedef results in (probably) what people expect, namely that both
x and y are pointers to short int.  The #define makes x a FLAGS and y
a short, probably not what you want.



More information about the Comp.lang.c mailing list