Let me ask why value of b is 1 after execution. | Sololearn: Learn to code for FREE!

+1

# Let me ask why value of b is 1 after execution.

int a=5; int b= a-a++;

10/19/2021 4:22:07 PM

Win Htay ðŸ‡²ðŸ‡²

+5

The answer is 0, not 1 Sololearn playground gives 1, but Cxxdroid gives 0. I guess it's like implementation defined. Better to not have side effects in expressions. Let's wait for some Pro to answer =)

+5

Different different compiler will give u different answer but if u writting ++a+a++ + --a or something like that then compiler get confused and also its unsafe so avoid with such type of calculation. Here Int a=5 ; int b=a-a++; always with pre increment or pre decrement first value increasing or decreasing then this value will be use in rest of expression . Here 5-5 get zero and assigned to b variable but after assigning a is increased by 1

+3

b= a - 5 // a++ = 5 b= 6 - 5 // a is increased from the last one b =1 Right to left (depends upon compiler)

+1

Dear Rishi, the result is actually 1. That is why i ask. I tried on 2 online compilers. Thanks dear.

+1

Many thanks dear #Omega. b = n++ - ++n; gives the answer -2.