0
#include<stdio.h> main() { int a=-5; int k=(a++,++a); printf("%d",k); }
Can anyone explain me why the output of this code is -3
1 Answer
+ 3
The comma operator evaluates its arguments from left to right, discards the one on the left, and finally returns the value on the right. It also inserts sequence points such that all side effects like incrementation are complete before evaluating the next expression.
https://en.cppreference.com/w/c/language/operator_other
So in this case, "a++" is evaluated and discarded, after which a = -4, then "++a" is evaluated, so that a = -3 now, and that value is then returned and assigned to 'k'.