I don't understand why, on this code, 'b+=' return 6 instead of 5. The operation on right-end of operator '+=', should be 0.
- i/2 = 4
- a-4= 0
So Operator '+=' should just add: 0.
#include<stdio.h>
int main(){
int a=4, i=8;
int b;
b=++a;
printf("%i\n",b);
b+=a-i/2;
printf("%i\n",b);
}
Just using theory of sintax
After this statement
bandawill be equal to5due to the prefix increment operator++.From the C Standard (6.5.3.1 Prefix increment and decrement operators)
So in this compound assignment statement
that may be rewritten like
you have
So as a result you have
bis equal to6.You could get the expected by you result if to initialize the variable
bby zeroand if to remove this statement
b=++a;