Operators

Pages: 12
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z,b=5;
clrscr();
x=b++ + ++b + b++;
y=b++ + b++ + ++b;
z=++b + b++ + b++;
printf("%d %d %d %d",x,y,z,b);
getch();
}


I know the output is 18, 27, 36, 14...

But I want to know the logic behind it, the order of evaluation...!!


PLEASE REPLY ASAP...
the order is not defined so you should not rely on it.
Recall the difference between pre-increment and post-incprement:
Pre-increment adds 1 to the value of the variable and returns a reference to it. Thus, ++i = x; is valid.
Post-increment first makes a copy of the original variable, increments the original, and returns the temporary copy. Thus, i++ = x; is not valid. The net effect is that it returns a temporary copy of the old value.

Pre- and Post-increment both happen before addition (http://www.cplusplus.com/doc/tutorial/operators/ ) and post-increment occurs before pre-increment. Post-increments are left to right order and pre-increment are right to left order.

So, b starts at 5.
x=b++ + ++b + b++;
First, we do post increment left to right which returns the old value:
x=5 + ++b + 6; b is currently at 7.
Then, we do pre-increment from right to left which returns the variable itself:
x=5 + 8 + 6; b is currently 8.
x now equals 19.

y=b++ + b++ + ++b;
Post-increment left to right:
y=8 + 9 + ++b; b is currently at 10.
Pre-increment right to left:
y=8 + 9 + 11; b is currently 11.
y now equals 28.

z=++b + b++ + b++;
Post-increment left to right:
z=++b + 11 + 12; b is currently at 13.
Pre-increment right to left:
z=14 + 11 + 12; b is currently 14.
z now equals 37

Thus, the C++ standard compliant results are 19 28 37 14.
Last edited on
L B it is not defined and so what you say is not true.

I get 18 25 36 14 when I run it.
http://www.cplusplus.com/doc/tutorial/operators/
In the table I see this:
2 ++ postfix Left-to-right
3 ++ unary (prefix) Right-to-left
3 + unary sign operator Right-to-left

Why is this information wrong?
As mentioned above:
https://secure.wikimedia.org/wikipedia/en/wiki/Sequence_point
In C and C++, evaluating such an expression yields undefined behavior.
I asked a completely different question:
LB wrote:
Why is this information wrong?

So, I will ask again: Why is the information on this site incorrect?
It's not incorrect.

But it's not addressing having more than one value change in one expression. Which is explicitly "defined" as undefined in C and C++.
That is perfectly correct information. The standard ALSO says that multiple modifications of a variable in between sequence points yields an undefined result (as is here).
If it is defined as undefined, why is there an order given in the table? This is very new to me.
Because this is not undefined:
1
2
int a=0;b=0;c=0;
int d = a++ + --b + ++c;
What if a, b, and c were functions that returned references to ints and printed out which one was called? Are you saying that by them simply being different it makes the order defined?
You are not allowed to change a variable more than once in an expression.
That's a completely different topic from evaluation order.

rocketboy uses a, b and c and not a, a and a. That's the point.
Last edited on
So, are you saying that order of operations is defined but the order in which the parts of the expression are evaluated is not? This is the only sense I can make of this...
What if a, b, and c were functions that returned references to ints[...]


As long as they are references to different ints it's OK.
Last edited on
No. I'm saying:
You are not allowed to change a variable more than once in an expression.
What order would the print statements show in the terminal?
The order of operations which produce the value of an expression is strictly defined. However, the order that the side effects ++ -- of an expression happen in is not, except in the case of the &&, || and ?: operators.
@L B

The table says it all:
a, c, b
Last edited on
Pages: 12