How this output came? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How this output came?

#include<stdio.h> int main() { int a = 10; printf("%d %d %d",a,++a,a++); return 0; } the output is 12 12 10 But how? Please Explain.

15th Oct 2018, 3:11 PM
Abhishek Barve
Abhishek Barve - avatar
3 Answers
+ 1
My bad. After reading this stackoverflow question, the function parameters aren't evaluated in a defined order. https://stackoverflow.com/questions/376278/parameter-evaluation-order-before-a-function-calling-in-c Meaning this code might give different results on different compilers.
15th Oct 2018, 3:31 PM
qwerty
qwerty - avatar
+ 1
It evaluates the arguments randomly. In this case, it executes a++ first which returns 10, then ++a which returns 12 and a which also returns 12.
15th Oct 2018, 3:27 PM
qwerty
qwerty - avatar
+ 1
in all the printf statements in c does it do calculation from right to left?
15th Oct 2018, 3:29 PM
Abhishek Barve
Abhishek Barve - avatar