Type bug in conditional expressions?
Dave Hanson
drh at cs.Princeton.EDU
Mon Sep 17 03:06:09 AEST 1990
In article <1606 at lupine.NCD.COM> rfg at NCD.COM (Ron Guilmette) writes:
In article <1990Sep12.151529.7268 at syd.dit.CSIRO.AU evans at syd.dit.CSIRO.AU (Bruce.Evans) writes:
gcc-1.37 on a 386 gives strange results for sizeof on conditional
expressions.
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 */
char e, f;
int g = sizeof (a ? e : f); /* 1, wrong? */
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.
It appears that (contrary to the normal rule about implicit promotions
of char & short expression operands to `int') that there are some special
rules which prevent such promotions when the operands are used as
arguments to sizeof().
Since these *exceptional* rules are never really spelled out very well,
I'm unable to decide what the `right' values for the expressions shown above are.
Can one of the comp.std.c gurus please say for sure what the `right' values
are?
the operands of ? (sec. 3.3.15) suffer the `usual arithmetic conversions'
(sec. 3.2.1.5), so the type of *all* of the sizeof operands above to be "int",
and the initial values of a,b,d,d,g are all 4.
the exceptions concerning sizeof are not transitive, eq, they don't apply
to the operands of ? in the examples above. the rules are better
explained in sec. 3.2.2. briefly, the operand of sizeof suffers few
(if any) of the usual conversions, eg, `array of T' is *not* converted
to `pointer to T', etc. so, for char c, sizeof c is 1, but sizeof (c+1) is 4.
More information about the Comp.std.c
mailing list