Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
C++
int main ()
    {
        int x, y,z;
        x = 1;
        y = ++x * ++x;
        cout << "x="<<x <<" y= "<< y;
        z = ++x * ++x  * ++x;
        cout <<"\nx="<< x<<" z= " << z;
        return 0;
    }



Output:
x=3 y=9
x=6 z=150
Posted
Updated 20-Aug-15 21:09pm
v2

Such an example shows one of the weirdest corners of C/C++. I wouldn't even suggest a beginner to have a look at it. However, if you are interested you might find pretty complete info here: "Undefined behavior and sequence points" at Stack Overflow[^].
 
Share this answer
 
It is a matter of knowing C language funny functionalities, it is very complicated to come with an explanation easy to comprehend for newbies.

Better than an explanation, see it running !

Use the debugger and see what it does, step by step.

For this code, I recommend to run the debugger at assembly level in order to split lines of code to their components. Be very attentive to the variables while executing.

The whole program is about playing with the tricky ++
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900