473,409 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

Why is the behavior different

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

Apr 4 '06 #1
3 1300
as***********@gmail.com 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)
Apr 4 '06 #2
as***********@gmail.com 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
Apr 4 '06 #3
Stefan Näwe wrote:
as***********@gmail.com 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.

Apr 4 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Andres Eduardo Hernando | last post by:
Hi, I'm not entirely sure this is the right group to ask this question, but I saw a similar one above, and the group's charter is not clear enough about it, so, here I go: ;) What is the...
5
by: jerars | last post by:
Hi! I have recently realized that some computer in my office have a different behavior when it comes to handle the onresize event in Internet explorer. I red a lot on this forum about this but I...
15
by: shablool | last post by:
Hi all, Two distinct compilers give different output for the following code: #include <iostream> #include <string> int main(void) { std::string s("0124"); s.replace(0, 3,...
2
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
6
by: Samuel M. Smith | last post by:
I have been playing around with a subclass of dict wrt a recipe for setting dict items using attribute syntax. The dict class has some read only attributes that generate an exception if I try to...
4
by: Yogi Watcher | last post by:
Hi, Recently I have observed some odd behavior of getenv and putenv function. I am developing some code that integrates with several other libraries. This program is not using MFC. It is plain C...
6
by: Joseph Geretz | last post by:
Writing an Outlook AddIn with C#. For the user interface within Outlook I'm adding matching pairs of Toolbar buttons and Menu items. All of the buttons and menu items are wired up to send events to...
111
by: Nate | last post by:
Hello, I am looking for a method to automatically declare variables in C. I'm not sure if there is a good way to do this, but I had something like this in mind... int i; for(i = 1; i < 4;...
2
by: Juha Nieminen | last post by:
Paavo Helde wrote: So which compiler is correct, gcc (which only requires bar() to be declared at the point of insantiation of foo()) or comeau? What happens if you just move the bar()...
1
by: =?Utf-8?B?TU5C?= | last post by:
Hi there! We are near the ending stage of an ASP.NET + IIS project, and started doing stress testing on the servers. We find ourselves with a very strange behavior, that can’t figure out since...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.