Tell me more ×
Computer Science Stack Exchange is a question and answer site for students, researchers and practitioners of computer science. It's 100% free, no registration required.
void main()
{
  int a = 1;
  a = ++a + ++a + ++a;
  printf("%d",a);
}

the above program gives the output 12.
What I have understood is that the variable 'a' is incremented thrice before it is cosidered for evaluation of the expression.

What I have'nt understood is why all the three increments are performed before any of them is evaluated in the expression.

Can anybody explain me with some simple equivalent low-level code about how the evaluatoin is done ?

share|improve this question
I think this question is more appropriate on stackoverflow, as it concerns real-world programming. I also think that the behaviour here is undefined, ++a "returns the value of a after incrementation" but the incrementation doesn't have to happen (but is allowed to) until the next sequence point. Check the C standard for details. – adrianN Feb 5 at 17:58
1  
@RaviTeja When you're blocked from posting questions on Stack Overflow, there no point in trying to bypass the block by asking on another site. Even if we wanted to, we cannot migrate the question to a site where you are blocked from asking. Read the link in the message that tells you that you are blocked for advice. – Gilles Feb 5 at 18:38
@RaviTeja See this answer on Stack Overflow: stackoverflow.com/a/4177063/71074 – Robert S. Barnes Feb 6 at 20:41
thanks... i was looking for those details. – Ravi Teja Feb 7 at 9:27

closed as off topic by Gilles Feb 5 at 18:37

Questions on Computer Science Stack Exchange are expected to relate to computer science within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

up vote 0 down vote accepted

That code is completely illegal, as it changes a several times between sequence points. Technically, that is undefined behaviour, the compiler can do whatever it pleases (and old gcc launched nethack in such cases).

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.