+ 2

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

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

19th Oct 2021, 4:22 PM
Win Htay 🇲🇲
Win Htay 🇲🇲 - avatar
5 odpowiedzi
+ 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 =)
19th Oct 2021, 4:37 PM
Rishi
Rishi - avatar
+ 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
19th Oct 2021, 4:48 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 3
b= a - 5 // a++ = 5 b= 6 - 5 // a is increased from the last one b =1 Right to left (depends upon compiler)
19th Oct 2021, 4:39 PM
Kashyap Kumar
Kashyap Kumar - avatar
+ 1
Dear Rishi, the result is actually 1. That is why i ask. I tried on 2 online compilers. Thanks dear.
19th Oct 2021, 4:40 PM
Win Htay 🇲🇲
Win Htay 🇲🇲 - avatar
+ 1
Many thanks dear #Omega. b = n++ - ++n; gives the answer -2.
19th Oct 2021, 4:46 PM
Win Htay 🇲🇲
Win Htay 🇲🇲 - avatar