Type bug in conditional expressions?

Doug Gwyn gwyn at smoke.BRL.MIL
Tue Sep 18 03:18:49 AEST 1990


In article <1606 at lupine.NCD.COM> rfg at NCD.COM (Ron Guilmette) writes:
>>int a = sizeof (1 ? (char) 1 : (char) 1);	/* 1, wrong? */
>>int b = sizeof (1 ? (short) 1 : (short) 1);	/* 2, wrong? */
>>int c = sizeof (1 ? (char) 1 : (short) 1);	/* 4, right */
>>int d = sizeof (0 ? (char) 1 : (short) 1);	/* 4, right */
>Even after re-reading section 3.3.3.4 of the ANSI C standard, I can't
>figure out what the proper values should be in the cases shown above.

I don't see what the problem is.  The value "sizeof expression" is the
number of bytes (== chars) that would be assigned by the implementation
for a variable having the type of the expression.  The type of a
conditional expression is well-defined (3.3.15).  In the case that the
second and third operands have arithmetic type, as here, the type of
the conditional expression has the common type produced by the usual
arithmetic conversions (see 3.2.1.5 for those rules).  In these examples
the integral promotions involved in the usual arithmetic conversions
result in either an int or unsigned int, both of which have the same
size (== sizeof(int)).  Thus if sizeof(int)==4, all the sizeofs in this
example should produce a value of 4.



More information about the Comp.std.c mailing list