#include<stdio.h> main() { int a=-5; int k=(a++,++a); printf("%d",k); } | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

4th Apr 2020, 11:35 AM
ADITYA PRATAP SINGH
ADITYA PRATAP SINGH - avatar
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'.
4th Apr 2020, 11:49 AM
Shadow
Shadow - avatar