delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/11/01/13:46:05

From: horst DOT kraemer AT t-online DOT de (Horst Kraemer)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Que on C
Date: Mon, 01 Nov 1999 18:10:56 GMT
Organization: T-Online
Lines: 57
Message-ID: <381db1e5.224896452@news.btx.dtag.de>
References: <7vjv87$32f$1 AT imsp026 DOT netvigator DOT com>
Mime-Version: 1.0
X-Trace: news00.btx.dtag.de 941479846 21181 0306239354-0001 991101 18:10:46
X-Complaints-To: abuse AT t-online DOT de
X-Sender: 0306239354-0001 AT t-dialin DOT net
X-Newsreader: Forte Free Agent 1.11/32.235
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

On Mon, 1 Nov 1999 19:54:22 +0800, "Jason News" <hello AT mymail DOT com>
wrote:


> Hi everyone,
> can anyone tells me why
> int x=5, y=5;
> printf ("1st %d, 2nd %d\n" ++x, y++");
 
> will show 1st 6, 2nd 5

Yes. That's correct.
 
> int x=5
> prinf ("1st %d, 2nd %d\n" ++x, x++");
 
> will show 1st 7, 2nd 5

What this statement will show is completely undefined. It may show
different results with different compilers and different results on
mondays and sundays and it may even not show anything because your
computer may crash.

The order in which arguments of an argument list are evaluated is
unspecified. Thus the compiler may choose to execute 'x++' and '++x'
in any order. This alone would make the output useless.

But the worst problem is that you are modifying x more than once in an
expression with no intervening "sequence points". In this case the
whole thing is just "wrong and illegal".

It's the same as if you would write the nonsense

	++x + x++

which is just garbage which casually compiles. The problem with C is
that not every syntactically legal construction will produce a
meaningful result.

A good rule is : Never use the variable name 'x' a second time in an
expression where x++ or ++x occurs. The only legal exceptions are
logical expressions like

	a = y<0 ? x : ++x;

or

	if ( x  || ++x )

where the different instances of 'x' appear at different sides of the
operation ( : || )



Regards
Horst

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019