🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.bug
Article #1164

Re: Type bug in conditional expressions?

#1164
From: Dave Hanson
Date: Sun, 16 Sep 1990 17:06
34 lines
2394 bytes
In article <1606@lupine.NCD.COM> rfg@NCD.COM (Ron Guilmette) writes:
    In article <1990Sep12.151529.7268@syd.dit.CSIRO.AU evans@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.
Technical Details
Message-ID: <2758@rossignol.Princeton.EDU>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rutgers!njin!princeton!cs!cs.Princeton.EDU!drh
References: <1990Sep12.151529.7268@syd.dit.CSIRO.AU> <1606@lupine.NCD.COM>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rutgers!njin!princeton!cs!cs.Princeton.EDU!drh
From: drh@cs.Princeton.EDU (Dave Hanson)
Newsgroups: gnu.gcc.bug,comp.std.c
Subject: Re: Type bug in conditional expressions?
Message-ID: <2758@rossignol.Princeton.EDU>
Date: 16 Sep 90 17:06:09 GMT
References: <1990Sep12.151529.7268@syd.dit.CSIRO.AU> <1606@lupine.NCD.COM>
Sender: news@cs.Princeton.EDU
Followup-To: gnu.gcc.bug
Distribution: gnu
Organization: Dept. of Computer Science, Princeton University
Lines: 34