[comp.lang.c] Summary of "What should sizeof

greim@sbsvax.UUCP (Michael Greim) (12/15/88)

Hello,

In <654@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@phoenix.princeton.edu (Raymond Juimong Chen)
cjc@ulysses.att.com (Christopher J. Calabrese)
friedl@vsi.com (Stephen J. Friedl)
tim@crackle.amd.com (Tim Olson)
mcdaniel@uicsrd.csrd.uiuc.edu (Tim McDaniel)
luis@rice.edu (Luis Soltero)
marv@ism780.isc.com (Marvin Rubenstein)
tanner%cdis-1%cdin-1@bpa.uucp (Dr. T. Andrews)
hansen%pegasus@att.uucp (Tony Hansen)
mccall@skvax1.ti.com (Fred McCall)
evil@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@sbsvax.informatik.uni-saarland.dbp.de
  (some mailers might not like this. Then use greim@sbsvax.uucp)
  or  : ...!uunet!unido!sbsvax!greim
# include <disclaimers/std.h>