Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Is this valid C? Date: Mon, 27 Feb 2017 10:01:09 -0800 Organization: None to speak of Lines: 34 Message-ID: References: <87mvdib11n.fsf@bsb.me.uk> <87d1e4alzc.fsf@bsb.me.uk> <87r32kkey5.fsf@news.bourguet.org> <87mvd88nfa.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="704c5d663da9e0150365451e4cbc43e2"; logging-data="30365"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+1c0YJXrKwgObRT2Nn0ss9" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:pyZAmV/IS0bvPEUXME+9JGR4Wfk= sha1:UMQ/C/94RwSoI/BFmIPIJTxTr/4= Xref: csiph.com comp.lang.c:104902 Ike Naar writes: > On 2017-02-27, BartC wrote: >> On 27/02/2017 13:51, Jerry Stuckle wrote: >>> Again, incorrect. With the ternary operator, only one of the two >>> subordinate expressions are evaluated, never both. >> >> If I compile: >> >> printf("%d\n", rand() ? 2+3 : 4+5*6); >> >> with gcc, then the output code contains both '5' and '34', the two >> possible expressions. >> >> So both have been evaluated in this case. > > Are you talking about both '5' and '34' appearing in a single run, > or '5' appearing in one run, and '34' appearing in another run? > > Can you show the complete code, plus the output of a few runs? He's talking about the generated assembly or machine code, not the run-time output. And he's assuming that if the constant 34 appears in the assembly code output, then the expression has been "evaluated". In fact, since 4+5*6 is a constant expression, it makes no difference in run-time behavior whether it's evaluated or not. If the output happens to be 5, then the generated code that loads the value 34 is not executed, and vice versa. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"