0

Possible Duplicate:
FAQ : Undefined Behavior and Sequence Points

#include<iostream>
#include<stdio.h>   
int main(){
   int myVal = 0;
   printf("%d %d %d\n", ++myVal,myVal,++myVal);
   myVal = 0 ; /*reset*/      
   std::cout<<++myVal<<" "<<myVal<<" "<<++myVal<<std::endl;
   return 0;
}

I got the output 2 2 2 in both the cases. How could it be 2 2 2? I expected 2 1 1 or 1 1 2

Community
  • 1
  • 1
Leon
  • 1
  • 7
    oh no. not again. search for `sequence point` in SO and you will get your answer – Chubsdad Nov 19 '10 at 11:22
  • 6
    Duplicate of [FAQ : Undefined Behavior and Sequence Points](http://stackoverflow.com/questions/4176328/faq-undefined-behavior-and-sequence-points) – Naveen Nov 19 '10 at 11:22
  • @Naveen: Thanks. Nowadays I don't like seeing that sequence 'sequence point' – Chubsdad Nov 19 '10 at 11:23
  • nobody answered my question. what is sequence point? – Leon Nov 19 '10 at 11:25
  • 2
    @Leon: you need to click on the link given by @Naveen and you will have plenty for today... – Chubsdad Nov 19 '10 at 11:26
  • 1
    @Chubsdad you are 100% correct but it might be worth pointing out that someone who is asking this kind of question probably isn't going to think to search for the term `Sequence points`... perhaps not even `Undefined Behavior`. Maybe we need a `What is this? I don't even...` tag :) – Moo-Juice Nov 19 '10 at 11:26
  • @Moo-Juice: Hmmm. I C (not C++ :)) your (sequence :) ) point. Thinking of 'sequence points' give me undefined behavior – Chubsdad Nov 19 '10 at 11:27
  • Rewrite your program to: _int myVal = 0; int myValA = ++myVal; int myValB = myVal; int myValC =++myVal; printf("%d %d %d\n", myValA, myValB, myValC)_;. As a general policy, assign your values first, then call printf. (This is not very readable, but people have closed your question before I could finish writing.) – Daniel Daranas Nov 19 '10 at 11:31
  • 1
    @Moo: You will not get people to search the FAQ extensively before they post here. And with the "what's the result of `i = ++i + i++`?-style questions they will have nothing they could search for except a bunch of special characters that all search engines choke on. OTOH, we now do have an [FAQ entry](http://meta.stackexchange.com/questions/68647/) for this that's easy to find and can be used to close such questions very quickly. Naceen did just this. I think this is as easy as it gets. – sbi Nov 19 '10 at 11:37
  • @Chubsdad: See my reply to Moo-Juice. – sbi Nov 19 '10 at 11:39
  • 2
    Now __who would vote to re-open such a clear-cut dupe?__ – sbi Nov 19 '10 at 11:40
  • @sbi: one of the UB trolls who keeps making new accounts to post silly repeats of this question? ;-) – R.. GitHub STOP HELPING ICE Nov 19 '10 at 15:16
  • @R..: Unlikely. IIRC, you need four digits of rep to cast votes on closing/re-opening. – sbi Nov 19 '10 at 16:35
  • Sorry, just a bad attempt to add some humor. – R.. GitHub STOP HELPING ICE Nov 19 '10 at 18:17

1 Answers1

0

The pre-incrementation operator is actually compiled so all calls to it are executed before the expressions calling printf and cout are evaluated.

It's just as if you had:

int myVal = 0;
myVal += 1;
myVal += 1;
printf("%d %d %d\n", myVal, myVal, myVal);

It could also imagined that compiler optimisations can go as far as using a constant '2' value instead of performing the incrementations at runtime in that case.

edit: diclaimer : this answer is an attempt at explaining what happened specifically in the case of the OP's code, but it really is an example of undefined behaviour, as compilers can do pretty much whatever they want in this situation.

SirDarius
  • 41,440
  • 8
  • 86
  • 100