Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why is the behavior different

Reply
Thread Tools

Why is the behavior different

 
 
ashish.dobhal@gmail.com
Guest
Posts: n/a
 
      04-04-2006
i=0;
printf("%d %d",i++,i);


I am writing the above piece of code and executing it on different
platforms (cygwin, solaris and linux). On cywin and linux the output is
"0 0", whereas on solaris it is "0 1". What the language specification
says about this case?


Thanks,
Ashish

 
Reply With Quote
 
 
 
 
Jakob Bieling
Guest
Posts: n/a
 
      04-04-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> i=0;
> printf("%d %d",i++,i);
>
>
> I am writing the above piece of code and executing it on different
> platforms (cygwin, solaris and linux). On cywin and linux the output
> is "0 0", whereas on solaris it is "0 1". What the language
> specification says about this case?


Afaik, it says its undefined, because you are reading and writing to
the same variable before reaching a sequence point. Just do not write
such code.

rhth
--
jb

(reply address in rot13, unscramble first)


 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Stefan_N=E4we?=
Guest
Posts: n/a
 
      04-04-2006
(E-Mail Removed) schrieb:
> i=0;
> printf("%d %d",i++,i);
>
>
> I am writing the above piece of code and executing it on different
> platforms (cygwin, solaris and linux). On cywin and linux the output is
> "0 0", whereas on solaris it is "0 1". What the language specification
> says about this case?


5.2.2.8:

The order of evaluation of arguments is unspecified. All side effects of argument expression evaluations
take effect before the function is entered. The order of evaluation of the postfix expression and the argument
expression list is unspecified.


Stefan
--
Stefan Naewe
naewe.s_AT_atlas_DOT_de
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      04-04-2006
Stefan Näwe wrote:

> (E-Mail Removed) schrieb:
>> i=0;
>> printf("%d %d",i++,i);
>>
>>
>> I am writing the above piece of code and executing it on different
>> platforms (cygwin, solaris and linux). On cywin and linux the output is
>> "0 0", whereas on solaris it is "0 1". What the language specification
>> says about this case?

>
> 5.2.2.8:
>
> The order of evaluation of arguments is unspecified. All side effects of
> argument expression evaluations take effect before the function is
> entered. The order of evaluation of the postfix expression and the
> argument expression list is unspecified.


However, this is not really significant here. The more important part is:

5/4:

Between the previous and next sequence point a scalar object shall have its
stored value modified at most once by the evaluation of an expression.
Furthermore, the prior value shall be accessed only to determine the value
to be stored.
The requirements of this paragraph shall be met for each allowable ordering
of the subexpressions of a full expression; otherwise the behavior is
undefined.


Since i is used with the %d format specifier above, it must be an integer,
for which operator++ doesn't give a sequence point, but it does modify i,
which is read again for the second argument. In a function call, there is
only a sequence point after _all_ arguments have been evaluated, not
between them. So the behavior is not unspecified, but undefined.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lists: why is this behavior different for index and slice assignments? John Salerno Python 7 04-24-2008 08:56 AM
Why do these two code blocks have different behavior? yuricake Ruby 3 05-05-2007 10:03 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
different behavior on different machines, same JRE/IE Johnny Ruin Java 5 03-15-2006 08:51 PM



Advertisments