Any one guess the output and how ?? | Sololearn: Learn to code for FREE!

+5

Any one guess the output and how ??

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

c++ c

4/1/2020 6:33:37 PM

Rohit Upadhyay

80 Answers

New Answer

+2

Its right...but how can u get this ??

+8

The result of the program is undefined by Standards, the expression suffers from an issue called "sequence point" which happens when the variable is accessed and modified more than once in an expression. Whatever result you get is compiler specific. Search net for more information on "sequence point".

+6

Rohit Upadhyay You can find a better explanation here😃 https://youtu.be/oM3W3Unl8GQ

+5

+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.

+2

Please read the last para of my comment. The result are compiler specific.

+2

Okk thanxx dude...

+2

The output is 3 2 3

+2

Output is 3 2 3

+2

Answer would be 1,2,2

+2

It depends on compiler.

+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

+1

It print 3 2 3

+1

Jayakrishna🇮🇳 Please read my answer again at the linked post. This undefined behavior thing is tricky one. I have updated my answer completely. I now believe my new answer is more complete/correct.

+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..?

+1

Jayakrishna🇮🇳 Yeah you are right, i was probably thinking too much :) I'll update it but it does produce sequence point (writing it that way). Rest of the explantion needs no more changes :)

+1

I don't understand please help me

+1

Well since the same output is called more than once , u get errors that says : variable a may be undefined. If u try it on another compiler maybe u don't get errors .

+1

it print 1 1 3

+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