Pre-increment and Post-increment | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Pre-increment and Post-increment

#include <stdio.h> int main() { int a=2,b=3; b=a++ + b--; printf("\n%d %d : a b",a,b); a=a-- + ++b; printf("\n%d %d : a b",a,b); b=++a + --b; printf("\nFinal values of a and b are %d %d",a,b); return 0; } Can anyone tell me the output of this code and please explain it to me! By solving on paper I have got a different value and the IDE shows the other. So by your answer, I can cross-check the mistake

5th May 2021, 6:26 AM
Ujwal Sai Simha Y
Ujwal Sai Simha Y - avatar
3 Answers
+ 1
Ujwal Sai Simha Y b=2+3=5 post-incre/decre a=3 b=5 a=3+6=9 pre-incre for 'b' b=6 and a=9 b=10+5 pre-incre for 'a' a=10 , pre-decre for 'b' b=5 final a=10 b=15
5th May 2021, 7:08 AM
Giriraj Yalpalwar
Giriraj Yalpalwar - avatar
0
@Thirt13n We get b=5 in the first condition and then will not be the post-decrement applied to it? I mean won't it be b=4 after post-decrement?
5th May 2021, 7:13 AM
Ujwal Sai Simha Y
Ujwal Sai Simha Y - avatar
0
Ujwal Sai Simha Y Increment/Decrement (++/--) has higher precedence than assignment (=) , first be will be decremented to 2 but after that it assigned with 5 , so b=5 ! Hope it will help you better !
5th May 2021, 7:22 AM
Giriraj Yalpalwar
Giriraj Yalpalwar - avatar