typedef vs #define

Walter Bright bright at Data-IO.COM
Tue Feb 27 06:15:07 AEST 1990


In article <8430 at cbnewsh.ATT.COM> em at cbnewsh.ATT.COM (edward.man) writes:
<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?

There is *no* difference in semantics for declarations such as:
	FLAGS f;
	short *pf = &f;
	FLAGS *a[10];
However, there is a difference in:
	unsigned FLAGS f;	/* error if FLAGS is a typedef */
There are also subtle differences between:
	{	unsigned short; }	/* if FLAGS is a macro	*/
and:
	{	unsigned FLAGS;  }	/* if FLAGS is a typedef	*/
Also, a symbolic debugger would know about the typedef, but not
the macro.

I'd recommend using the typedef unless there is a very good reason not to.



More information about the Comp.lang.c mailing list