Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed int a=2; printf("%d", ++a + a++);

+26 votes
asked Jun 18, 2019 by kj
closed Dec 27, 2021 by Admin
closed with the note: answered

38 Answers

+4 votes
answered Jun 19, 2019 by anonymous
commented Jun 21, 2019 by anant saurabh (100 points) 1 flag
right ans is 6
commented Jun 26, 2019 by Kenil Kumar (110 points) 1 flag
yaa right answer is 6 bcz first it will make a = 3 bcz of preincrimnet and the it will add 3 to 3 as a++ is post increment so it's answer will be 6
commented Jun 27, 2019 by anonymous 1 flag
correct answer is 6
commented Jul 1, 2019 by gowtham 1 flag
first a is incremented by one because that ++a first interment then stored a++ is first stored then is incremented.so 2 incremented 3 now a value is 3 then a++ first stored 3 does not incremented so 3+3=6.
commented Jul 3, 2019 by Hamza Khan (100 points) 1 flag
the answer is 7 because when u code this the answer is shown as 7, this is because the first incrimination (++a) is 3 then the second one becomes 4 as a=3 for the second incrimination so it becomes 3+4=7
commented Jul 4, 2019 by anonymous
commented Dec 22, 2019 by kotha kushal (560 points)
no the out put will be a garbage value
commented Jan 24, 2020 by nagnet (100 points)
It will be the 7.
commented Dec 26, 2021 by Peter Minarik (69,640 points)
The expression is not a legal C expression as the C standard does not specify in what order this needs to be executed, hence the evaluation depends on the compiler itself. Just try the GNU C, Turbo C or Microsoft C and you can see the result differs for the same code.

Hence the answer is: UNDEFINED BEHAVIOUR
–1 vote
answered Jun 20, 2019 by anonymous
'5' will be the answer.
0 votes
answered Jun 20, 2019 by anonymous
'7' is the answer.
0 votes
answered Jun 20, 2019 by Pirai (140 points)
6 is the answer
–1 vote
answered Jun 20, 2019 by half_deadlock

++a define a=a+1, So it's value is a=3

a++ define only a+1 but it noe store into a so value of a is 2

so 3+2=5

so final answer is 5
commented Jun 21, 2019 by anonymous
Sorry to say but you are wrong.
Value of a is 2 at first
For ++a, the value of a is icremented by 1 immediately,so it becomes a=2+1=3
So, the new value of a is 3 now
the trickiest part now ---------
therefore, (++a)+(a++) = 3 + 4 = 7
so 7 will be the answer
–1 vote
answered Jun 22, 2019 by RACHANA PARVATIKAR (120 points)
output of this is 5
–1 vote
answered Jun 22, 2019 by nandhini (160 points)
Output for this is 7

a has value 2 the it get incremented as 3 add to the post incremented value 4 then the answer is absolutely 7.
–1 vote
answered Jun 22, 2019 by Prasanna Kumar (120 points)
initially pre-increment operation occurs, ++a=3, then substiution of the values takes place,i.e (++a + a++)=(3 + 3) =6 then post increment occurs hence a becomes 7

the ans is 7
–1 vote
answered Jun 25, 2019 by Harshit 1 flag
ANSWER is 6 because 3+3=6
–1 vote
answered Jun 25, 2019 by Baskar (120 points)
answer will be 7
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.