Summary of "What should sizeof(expression) return? Why 8?"
Michael Greim
greim at sbsvax.UUCP
Thu Dec 15 23:55:39 AEST 1988
Hello,
In <654 at sbsvax.UUCP> I asked several questions about what sizeof(expression)
should return and why it returned certain values.
This is a summary of responses.
The questions were (printing was done on a VAX 11/780, running 4.3BSD)
1.) what is sizeof(bitfield of length 1)?
Printed : 4
2.) what is sizeof(c1<c2), where "char c1, c2"?
Printed : 4
3.) what is sizeof (i+r), where "int i; float r;"?
Printed : 8
4.) what is sizeof (i+s), where "int i; char s [20];"?
I got answers from
rjchen at phoenix.princeton.edu (Raymond Juimong Chen)
cjc at ulysses.att.com (Christopher J. Calabrese)
friedl at vsi.com (Stephen J. Friedl)
tim at crackle.amd.com (Tim Olson)
mcdaniel at uicsrd.csrd.uiuc.edu (Tim McDaniel)
luis at rice.edu (Luis Soltero)
marv at ism780.isc.com (Marvin Rubenstein)
tanner%cdis-1%cdin-1 at bpa.uucp (Dr. T. Andrews)
hansen%pegasus at att.uucp (Tony Hansen)
mccall at skvax1.ti.com (Fred McCall)
evil at arcturus.UUCP (Wade Guthrie)
(Quite a list, eh? :-)
Everybody had the right answers, of course. I quote here the answer by
Tony Hansen:
>Simple. sizeof(expression) returns the size of the type of the expression.
>In particular, the one which confused you was (i+r), adding an integer to a
>float. Evidently your compiler takes the legal option of calculating all
>floating-point expressions in double-precision. Therefore (i+r) is of type
>double and sizeof returns 8. All K&R compilers did this conversion to
>double-precision; ANSI C permits the calculation to be performed in
>single-precision if the answer can be guaranteed to be exactly the same.
The reason why I asked it in the first place was that I did not find the
appropriate paragraph in K&R, so I thought that the return values were
at least machine dependent and quite certainly undefined by C.
Thanks to all the people who helped to educate me on this topic.
-mg
--
email : greim at sbsvax.informatik.uni-saarland.dbp.de
(some mailers might not like this. Then use greim at sbsvax.uucp)
or : ...!uunet!unido!sbsvax!greim
# include <disclaimers/std.h>
More information about the Comp.lang.c
mailing list