Order of evaluation of this expression

1
2
3
4
int x, *y;
x=10;
y=&x;
++x = -3543 * 543 % 2 | 4 > 42 && 324 * !423 / ~31 << 3  * *y * sizeof(x);


How is this expression evaluated? Please explain step by step.
I'm having lots of trouble regarding order of evaluation of an expression.
Please help!

Thanks a lot!!
Here's a good link to start with: http://en.cppreference.com/w/cpp/language/operator_precedence

That will give you the order of operations for C++. From there you just need to work it out, really. This looks to me like a homework problem, so don't expect someone to do it for you, but we'll be happy to help you if you get stuck. Work on it and post what you get done and we'll be happy to help you out. Good luck.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int x, *y;//x is declared as an integer y is declared as a pointer
x=10;//Set the value of x to 10
y=&x;//y is pointing to the value at the address of x so the value of y is now 10 also
++x = -3543 * 543 % 2 | 4 > 42 && 324 * !423 / ~31 << 3  * *y * sizeof(x);
std::cout <<x <<std::endl;

//++x <---immediately increment x i.e. x is 11 at the start of this assignment
//And from here on, the gnashing of teeth begins...I will explain 
//in chunks starting at the end
//Note that what I explan might not be exactly what 
//this code will do (WHICH IS WHY YOU NEED BRACKETS PEOPLE!!)
//*y * sizeof(x) = 11 * 4 (11 because remember that y 
//is pointing to the value at the address of x (44)
//~31 << 3 * 44 = shift the bits of 31 to the left 3 places this actually turns out to be
//0001111 << 3 ---> 1111000 ---> 240 * 44 (10560)
// !423 / 10560 ---> -423 / 10560 ---> (-0.0401)
//324 * -0.0401 ---> since we are dealing with integer, this evaluates to 0
//4 > 42 && 0 ---> false or 0 ---> 0
//doesn't matter what we do from here, the answer is that x is 0
//I say this because next we are going to do a bitwise OR
//And in bitwise OR, anything Or 0 equals 0
//543 % 2 | 0 --->Bitwise Or (anything Or 0 equals 0) this evaluates to 0
//-3543 * 0 == 0
//x = 0 


Like I mentioned, whoever did this did not want to get a real answer, just wanted to troll hard. I tried compiling this and I get a warning:

$ g++ -Wall -Wextra -o meh mah.cc
mah.cc: In function β€˜int main()’:
mah.cc:8:29: warning: suggest parentheses around comparison in operand of β€˜|’ [-Wparentheses]


Here is the result when you run it

$ ./meh
0
Last edited on
Thanks randisking and Smac89 :)..

But could you explain what associativity really means? with an example
method of evaluation

ex int x = 3 + 4; is evaluated left to right. First int x is declared, then '=' sets x to whatever value follows which is 3 + 4 = 7
> I'm having lots of trouble regarding order of evaluation of an expression.

Order of evaluation of the operands of any C++ operator, including the order of evaluation of function arguments in a function-call expression, and the order of evaluation of the subexpressions within any expression is unspecified (except where noted below). The compiler will evaluate them in any order, and may choose another order when the same expression is evaluated again.

There is no concept of left-to-right or right-to-left evaluation in C++, which is not to be confused with left-to-right and right-to-left associativity of operators: the expression f1() + f2() + f3() is parsed as (f1() + f2()) + f3() due to left-to-right associativity of operator+, but the function call to f3 may be evaluated first, last, or between f1() or f2() at run time.

http://en.cppreference.com/w/cpp/language/eval_order
Smac89 wrote:
//doesn't matter what we do from here, the answer is that x is 0
//I say this because next we are going to do a bitwise OR
//And in bitwise OR, anything Or 0 equals 0
//543 % 2 | 0 --->Bitwise Or (anything Or 0 equals 0) this evaluates to 0


That's not correct.

1
2
3
4
5
    int a = 1234;
    int b = 0;
    int c = a | b;

    cout << "a: " << a << "  b: " << b << "  c: " << c << endl;


a: 1234  b: 0  c: 1234

Topic archived. No new replies allowed.