Any one guess the output and how ?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Any one guess the output and how ??

a=1 Printf("%d %d %d",a,a++,++a)

1st Apr 2020, 6:33 PM
Rohit Upadhyay
Rohit Upadhyay - avatar
72 Answers
+ 2
Its right...but how can u get this ??
3rd Apr 2020, 4:09 PM
Rohit Upadhyay
Rohit Upadhyay - avatar
+ 5
1 2 3
2nd Apr 2020, 6:24 AM
ekhlas Mohamed Abdallah Mohamed
ekhlas Mohamed Abdallah Mohamed - avatar
+ 3
It is compiler dependent. But if u run it in gcc output will be 3 2 3 If u try to figure out how it come U have know the basics of assembly language.
2nd Apr 2020, 8:35 AM
Shubham Kumar
Shubham Kumar - avatar
+ 2
Okk thanxx dude...
1st Apr 2020, 6:46 PM
Rohit Upadhyay
Rohit Upadhyay - avatar
+ 2
The output is 3 2 3
2nd Apr 2020, 7:14 AM
T. Siva Kiran
T. Siva Kiran - avatar
+ 2
Output is 3 2 3
2nd Apr 2020, 12:48 PM
Ravishan Fernando
Ravishan Fernando - avatar
+ 2
Answer would be 1,2,2
3rd Apr 2020, 3:42 AM
xkcd
xkcd - avatar
+ 2
It depends on compiler.
3rd Apr 2020, 7:51 AM
Thirumahal
+ 1
Output is undefined by standards.. Means it give different outputs on different compilers.. compiler specific. See this for explanation... https://www.sololearn.com/Discuss/2038766/?ref=app
1st Apr 2020, 6:36 PM
Jayakrishna 🇮🇳
+ 1
It print 3 2 3
1st Apr 2020, 6:40 PM
Rohit Upadhyay
Rohit Upadhyay - avatar
+ 1
~ swim ~ I checked that code on that linked post when I answered there but I was not sure after seeing there your and another previous replies. So I just mentioned in java and added about c++ later so just relate to c++..... Edit: Here I just added link for alternative explanations.. I have only one that. I thought to add more, but stopped after seeing your reply.. Not answered more.. I thought to delete also, because you added enough explanation... All fine now..?
1st Apr 2020, 8:20 PM
Jayakrishna 🇮🇳
+ 1
I don't understand please help me
2nd Apr 2020, 4:44 AM
raguvardhan
+ 1
it print 1 1 3
2nd Apr 2020, 2:25 PM
Lavanya T
Lavanya T - avatar
+ 1
printf("%d",a); printf("%d",a++); printf("%d",++a); this way output is 1 1 3 but when we write in this way printf("%d %d %d",a,a++,++a); OUTPUT 3 2 3 this way lead to" sequence point" problem as mansioned before
2nd Apr 2020, 5:09 PM
Mahmoud Abo Elsaoud
Mahmoud Abo Elsaoud - avatar
+ 1
3 2 2 is the correct answer
2nd Apr 2020, 6:10 PM
Bhagyashri Kothawade
+ 1
Language problem
3rd Apr 2020, 5:44 AM
xkcd
xkcd - avatar
+ 1
I tried in different two compilers in solo its giving 323 but in other 113 . Its a languages dependent . In cpp output is 113
3rd Apr 2020, 7:14 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
113
3rd Apr 2020, 7:50 AM
Lavanya T
Lavanya T - avatar
+ 1
See printf() uses stack. Means your last argument is treated first then 2nd last argument at second place…. first argument is treated at last. Step 1: last argument is treated first. ++a becomes 2 but that last is not assigned value of 2 yet. It’s waiting for whole statement to execute then assign value. Yet we get value of a as 2 Step 2: Now its time to treat 2nd or I can say 2nd last argument. Told you post increment instantly assigns value then do increment operation. So 2nd argument gets value assigned to 2. Increment is now done and value of a turns out to be 3. Step 3: Now its time to treat the first argument. It simply gets value assigned 3. Now that all the operations are performed so it’s time for pre-increment to do its left out work(assignment of value). So last argument gets value of 3. So answer of your query is : 3 2 3 And one more thing to remember:The answer may vary compiler to compiler. Many compiler have different implementations of printf().
3rd Apr 2020, 9:50 AM
Sam
Sam - avatar
+ 1
its printing 3 2 3.I think it first starts with ++a and a=2, then it goes to a++ and a=3 but post increment return values befor expression so a++ is 2 and ++a and a returns 3 after all updates.
3rd Apr 2020, 3:43 PM
Shubham Yadav
Shubham Yadav - avatar