Path: utzoo!attcan!uunet!tut.cis.ohio-state.edu!purdue!haven!adm!smoke!gwyn From: gwyn@smoke.BRL.MIL (Doug Gwyn) Newsgroups: comp.std.c Subject: Re: Type bug in conditional expressions? Message-ID: <13890@smoke.BRL.MIL> Date: 17 Sep 90 17:18:49 GMT References: <1990Sep12.151529.7268@syd.dit.CSIRO.AU> <1606@lupine.NCD.COM> Organization: U.S. Army Ballistic Research Laboratory, APG, MD. Lines: 19 In article <1606@lupine.NCD.COM> rfg@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.