sizeof() confusion

Adam S. Denton asd at cbnewsj.att.com
Thu Nov 15 02:42:13 AEST 1990


In article <2692 at cirrusl.UUCP>, dhesi%cirrusl at oliveb.ATC.olivetti.com (Rahul Dhesi) writes:
> 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.

You take sizeof() of an object, not an expression.

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

Your view is nice, but is not C.

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

It is the responsibility of the programmer to know the language
and use it properly, whether the language is perfect or not.
A hammer works quite well driving nails.  It's not the hammer's
fault if I have difficulty drilling holes with it.  C works
quite well with sizeof() and promotions defined the way they are.
Perhaps they could be better, but widely-available C compilers
do them the current way -- for good or ill.  If I want to avoid
writing my own compiler and my own non-portable code, I had better
code using what's out there and accepted NOW.  So (IMHO) no one
should EVER be "surprised" at C.  If they are, then they haven't
read and/or understood the manual or the Standard or whatever applies.
To use a tool properly, you must know how to do so.  C is a tool.

Adam Denton
asd at mtqua.att.com



More information about the Comp.lang.c mailing list