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


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 Answers

New Answer


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 =)


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


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


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


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