0
#include<stdio.h> main() { short a,b; a=2; b=5; b=a++ +b+a; printf("Output %d\n",b);//9 printf("Output %d\n",a);//3 }
Please can anyone explain that why variable "b" is storing 10 instead of 9.
3 Answers
+ 5
First read about the incremet and decrement operator how its woRkiNg.
Pre incremet will solve first then expression will calculate but in case of post incremet or decrement expression will calc then value will increase or decrease.
In your case here u defined a =2 and b=5
In next line b=a++ + b+a; here u used Post increment so first expression will calculate then a will be increase
So b= a++ + b+a
b=2+5+3 here after 2+5 +a here value will increase 3
b=2+5+3 =10 but its depend on Compiler may be some Compilers or different online Compiler will give you different different Outputs . May be some will give 9 .
+ 1
a++ +b +a=2+5+3 ++ increments the value of variable a later in the expression
+ 1
One of the classic "Undefined Behavior". In some compilers, it will be evaluated as b = 2 + 5 + 2, the others will evaluate it as b = 2 + 5 + 3.