Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

BUG in VC++ ? :()

Posted on 1998-11-10
6
Medium Priority
?
181 Views
Last Modified: 2010-04-02
well, here goes the C++ question

  Why does the following code produce 211 as the answer

  int i= 1;
  cout << i << i++ << i << endl;

0
Comment
Question by:umzilber
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 800 total points
ID: 1177467
The order of evaluation of the arguments to cout is not defined by the C++ language standard.  In other words, you cannot be assured that just because you have placed a variable in the left most position in the statement, that it will get evaluated in any particular order.
0
 
LVL 32

Expert Comment

by:jhance
ID: 1177468
BTW, it appears in your example above that the order of evaluation is from right to left.  This makes the right most i = 1, the middle one is also = 1, it is then incremented and then the left most i is now = 2.
0
 
LVL 32

Expert Comment

by:jhance
ID: 1177469
Just another thought.  This is much like the C statement:

printf("%d%d%d\n", i, i++, i);

What will this statement print out?  It might be 211, it might not be, it's not defined.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 22

Expert Comment

by:nietod
ID: 1177470
>>order of evaluation of the arguments to cout is not defined by the C++ language standard
This has nothing to do with the cout.  it has to do with the order of evaluation of operands in an expression and it IS defined by the standard.  This will work the same on all C++ compilers (assuming they don't have a bug).  This will work with the >> operator in all cases.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177471
I take that back.

The order in which subexpressions are evaluated is implimentation defined (except with "," ,"&&", and "||").  You are guarranteed that the << operators are called in left to right order, but the values that are passed to them might not be.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177472
To make that clearer.  in

cout << i << i++

You are guaranteed that the order of evaluation of the << operators is

(cout <<  i) << i++

but you are not guarantted the order in which the i and i++ subexpressions are calculated.  The only guarantee that you get is that the second call to operator << will have the value of i before it  was ++ed there is no guarantee that the ++ is done after the call to the 2nd operator <<, what's mroe there is no guarantee that is done after the call to the 1st!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question