I am trying to understand the priorities of operations in the following example:

int a = 2; int b = 3; int c = 9; a += --a + a * ++a; c += --b != 3 > ++c; b = ++c + --a == 3 * ++b != ++c + --b <= ++b; 

a += --a + a * ++a; in Eclipse a += --a + a * ++a; - gave the value of the variable a equal to 5, but when I consider myself, I get a different value of this variable.

Please explain to me each step of the execution of the codes given by me.

  • So what values ​​are obtained after evaluating expressions, and what values ​​should be in your opinion ?! - Vlad from Moscow
  • not minus 5, but just 5. in principle, everything is logical. Although such constructions are fanaticism - Alexey Shimansky
  • There is not a minus, but just a dash. I considered it this way (in the example: a + = - a + a * ++ a ;, then it just didn’t make sense to climb because of misunderstanding): first the increment ++, then the decrement - (if they are in front of the variable as I remember, they have the highest priority of all operators in Java, because I started to calculate them first), multiplication and addition, as a result, the result of 8. - SpaceWanderer
  • It is on one educational site, a test like that. - SpaceWanderer
  • Thank you for responding. - SpaceWanderer

2 answers 2

Here is the same disassembled C # code:

  int a = 2; 01812DC5 mov dword ptr [ebp-40h],2 ; a = 2 a += --a + a * ++a; 01812DCC mov eax,dword ptr [ebp-40h] ; eax = a(2) 01812DCF mov dword ptr [ebp-48h],eax ; buf1 = eax(2) 01812DD2 dec dword ptr [ebp-40h] ; a = a(2) - 1 01812DD5 mov eax,dword ptr [ebp-40h] ; eax = a(1) 01812DD8 mov dword ptr [ebp-4Ch],eax ; buf2 = eax(1) 01812DDB mov eax,dword ptr [ebp-40h] ; eax = a(1) 01812DDE mov dword ptr [ebp-50h],eax ; buf3 = eax(1) 01812DE1 inc dword ptr [ebp-40h] ; a = a(1) + 1 01812DE4 mov eax,dword ptr [ebp-48h] ; eax = buf1(2) 01812DE7 add eax,dword ptr [ebp-4Ch] ; eax = eax(2) + buf2(1) 01812DEA mov edx,dword ptr [ebp-40h] ; edx = a(2) 01812DED imul edx,dword ptr [ebp-50h] ; edx = edx(2) * buf3(1) 01812DF1 add eax,edx ; eax = eax(3) + edx(2) 01812DF3 mov dword ptr [ebp-40h],eax ; a = eax(5) 

Those. the state of variable a changed as follows:

 a += --a + a * ++a; // a = ? 2 += --a + a * ++a; // a = 2 2 += 1 + a * ++a; // a = 1 2 += 1 + 1 * ++a; // a = 1 2 += 1 + 1 * 2; // a = 2 2 += 1 + 2; // a = 2 2 += 3; // a = 2 5; // a = 5 

    In general, but you did not specify a compiler, compilation flags, and in general a language - the correct answer is one: "undefined behavior".

    The increments / decrements themselves are usually calculated in the first place (although only usually, a particular standard of a particular language may have a different opinion), but in what order among themselves is the question.

    For example, the expression

     --a + a * ++a 

    It can be done in the order: --a, ++ a, *, +. Maybe ++ a, *, +, --a, maybe ++ a, --a, *, +. The results, of course, will be different. But all at the same time true. Which option will be used depends on:

    • the standard of the language used, there may explicitly specify the order of evaluation of the operands
    • specific compiler
    • specific version of the compiler
    • and even specific compilation flags
    • Well, if not from the phase of the moon and the temperature on Mars, i.e. at least on a specific compiler is reproduced from assembly to assembly

    It is called indefinite behavior. For such a code, it is very painful to beat hands, but it is not uncommon at all interviews.