Could you explain the output of this code. ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Could you explain the output of this code. ?

#include <stdio.h> int main() { int x=10; printf("%d \n",x + x++ ); x=10; printf("%d \n",x + x++ + x++); x=10; printf("%d \n",x + x++ + x); x=10; printf("%d \n",x + x++ + x+ x); x=10; printf("%d \n",x + x++ + x++ +x); x=10; printf("%d \n",x + x++ + x++ +x++); return 0; } Output: 21 32 32 43 44 44

13th Sep 2021, 7:42 AM
Md. Eliyas Akondo
Md. Eliyas Akondo - avatar
2 Answers
+ 1
printf("%d \n",x + x++ ); // x = 10, x++ = 11 so sum is 21. (10+(10+1) = 21) x=10; printf("%d \n",x + x++ + x++); // x = 10, x++ = 11 and , sum is 32. You may think why sum isn't 33, cause you add x first after you increment its value. (10+(10+1)+11 = 32) + 1 but after printing so it is not calculated while it displays. x=10; printf("%d \n",x + x++ + x); // x = 10, x++ = 11 and x = 11 again, sum is 32. This statement the same with previous statement.(11+(10+1)+11 = 32) x=10; printf("%d \n",x + x++ + x+ x); // x = 10, x++ = 11 now and the final value of x is 11 so sum is 43. (10+(10+1)+11+11 = 43) x=10; printf("%d \n",x + x++ + x++ +x); // x = 10, x++ = 11 now and final value of x is 12. So, sum is 44. (10+(10+1)+(11)+12 = 44) x=10; printf("%d \n",x + x++ + x++ +x++); // x = 10, x++ = 11 and x++= 12. (10+(10+1)+(11+1)+12 = 44) +1 but after prints this statement so it is not calculated.
13th Sep 2021, 8:17 AM
mesarthim
mesarthim - avatar
0
In that case value are not changing in last two statements but why? x and x++ are showing the same result.
13th Sep 2021, 7:50 AM
Md. Eliyas Akondo
Md. Eliyas Akondo - avatar