Why are typedef names in the same name space as variable names?

Kenneth R. Ballou ballou at brahms
Sun Nov 16 09:50:11 AEST 1986


In article <1092 at spice.cs.cmu.edu> jgm at spice.cs.cmu.edu (John Myers) writes:
>In article <4220 at ut-ngp.UUCP> dlnash at ut-ngp.UUCP (Donald L. Nash) writes:
>>Why are typedef names in the same name space as variable names?...
>
>If they weren't, then what would the output of the following program be?
>
>#include <stdio.h>
>main()
>{
>    typedef car foo;
Is this a typo, and supposed to be typedef char foo ?
>    long foo;
>    
>    printf("%d\n",sizeof(foo));
>}

I don't see that there is a question here.  Sizeof is *NOT* a function, as was
quite thoroughly discussed some time back in this group.  Note that (foo) in
this case is the syntax of a type cast, and in this case, sizeof returns 
the size of a datum of type foo (in this case, 1).  If, however, you were to
write  sizeof foo , you would then get the size of a long integer on your
machine.  References:  K&R:  page 126 (explains sizeof (object) and also
points out that sizes are given in unspecified units called "bytes," which
are the same size as a char); page 187 (gives two forms of the *operator*
sizeof, namely  sizeof expression  and  sizeof (type-name) ); page 188 (defines
the meaning of the constructs involving sizeof and also notes that "byte"
is the space required to hold a char *in all existing implementations*).

--------
Kenneth R. Ballou		...!ucbvax!brahms!ballou
Department of Mathematics	ballou at brahms.berkeley.edu
University of California
Berkeley, California  94720



More information about the Comp.lang.c mailing list