main ()

{

int x=5,y;

y = x++ + x++ ;

}

what is the value of y?

its coming as 10 bt is should be 5 +6 =11 … !! whats happening ?

main ()

{

int x=5,y;

y = x++ + x++ ;

}

what is the value of y?

its coming as 10 bt is should be 5 +6 =11 … !! whats happening ?

x++ --> Post incrementation so x will be incremented after execution of current statement …

y = x++ + x++; is same as y = x + x; x++; x++;

so for example if initially x = 10, then after executing that statement y = 10 + 10 = 20 and x = 12

Edit: Its not 10 + 11 because post incrementation will happen after executing complete expression

1 Like

Hello,

I read the following about the postfix ++ operator in a draft ISO standard for C (I do not own a copy of a final version):

“The side effect of updating the stored value of the operand shall occur between the previous and next sequence point.”

In this example, that can be anywhere in the assignment. Therefore, in this example, the resulting value is undefined. By experimenting you can try to see what will happen in a specific compiler using specific optimisation settings, but I would advise you not to depend on undefined behaviour.

Edit: I found a blog post to back up the statement that the behaviour is undefined: http://blog.cdleary.com/2010/01/two-postfix-operations-redux-sequence-points/

so what about ++x + x++ + x++ ? nd how is the flow of control directed. ?

either x++ is evaluated first or ++x ?

x=10;

y = ++x + x++ + x++

y = 33 and x = 13

Flow??

++x is Pre incrementation and x++ is post incrementation. So when it is ++x, before the statement is executed x is incremented.

And so following 2 results in same answer

y = ++x + x++ + x++

y = x++ + ++x + x++

Here is something interesting …

x=10

y = ++x + x++ * x++ ==> y=132,x=13

y = x++ * x++ + ++x ==> y=111,x=13

I think it will result in 11 in Borland turbo C++ 16 bit compilers … which were used earlier.

x++ is post increment ie use then change and ++x is change then use…so for x++ 5 is used and for ++x 6 is used while performing the operation