I don't understand in which order actions take place in C++ expressions.
For example: Why do we get here -129? Can you explain order of actions?
#include <stdio.h>
int main()
{
char a = 60;
unsigned c = 88;
long d = 134;
int e = -6;
printf("Reuslt: %ld\n",!a++<sizeof(long double)&(c)|~d--+-e );
return 0;
}
I tried to do this with actions using int k1, k2, k3, ..., kn(where k stands for 1 action) , but i don't understand order of actions yet.
I will not provide you the exact answer because this is an exercise and ultimately it is you who needs to solve it. But I will give you the information you need in order to solve it.
Priorities:
!: 3a++: 2<: 9sizeof(): 3a&b: 11|: 13~: 3d--: 2+: 6-e: 3Source: https://en.cppreference.com/w/cpp/language/operator_precedence
One would expect the order to be left-to-right when we speak about operations of the same priority (except special cases, like assignment, which is not present here and where we go right-to-left), an order overriden by the priorities of the operations. The lower the priority number, the higher the priority of the operation is.
For example,
!a++will execute the++first and only then the!, because++is of higher priority (lower prio number means a preceding operator). Therefore, you are well-advised to take a pen & paper and draw the expression tree.This will allow you to understand what exactly happens here and in what order. Try to figure this out without peeking at the actual result of the expression and compare the result you get without being influenced with the result you test later using the expression. If the results are matching, then it's likely (but not 100% proven) that you were correct. If the results do not match, then something's off.
If the results are mismatching, then isolate subtrees of the expression tree and evaluate them separately until you get the exact reason(s) that caused the mismatch and then you will understand why you were wrong.