sizeof() confusion

Rahul Dhesi dhesi%cirrusl at oliveb.ATC.olivetti.com
Wed Nov 14 08:26:57 AEST 1990


>>        printf("sizeof c       = %d\n", sizeof c);
>>        printf("sizeof 'c'     = %d\n", sizeof 'c');

>Were you suprised that "'c'" was 4?
>You shouldn't be.  That expression evaluates to an int, not a char.

If characters are promoted to ints in expressions, then why isn't
|sizeof c| equivalent to |sizeof (int) c|?  The confusion arises
because the term "expression" is defined differently in the definition
of C and in colloquoal conversation.

>From my point of view -- call it naive if you will -- anything
that has a value is an "expression".  Therefore, if |c| as used above
has a value, it's an expression.  Therefore |c| must be promoted to
int.  Therefore |sizeof c| is equivalent to |sizeof (int) c|.  Hence
the surprise.

I'm sure K&R, H&S, and the ANSI standard all define these things in
various places.  But they can only guarantee what things mean, not
whether the meanings they define will surprise programmers.
--
Rahul Dhesi <dhesi%cirrusl at oliveb.ATC.olivetti.com>
UUCP:  oliveb!cirrusl!dhesi



More information about the Comp.lang.c mailing list