Notice: Undefined offset: 260497 in /var/www/html/qa-external/qa-external-users.php on line 744
int a=2; printf("%d", ++a + a++); - OnlineGDB Q&A
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++);

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

38 Answers

+5 votes
answered Jun 19, 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 (101,570 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
commented May 30, 2023 by Aadi k (100 points)
a = 2
a++ = 3
++a = 3

++a + a++ = 6

answer is 6
commented Jun 28 by IFEOLUWA OMIKUNLE (620 points)
yeah answer is 6
–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.
commented Oct 26, 2025 by Teja (110 points)
no bro it is 6
printf("%d",++a + a++);
firstly the ++a adds 1 before using a
so the value of a will become 3 and then that value is used.
now it is added to a++,
here the value of a is used first and then increment will happen
so (3 + 3) {To be noted that the value of a is 4 now )
answer is 6
thats it. it is simple bro
even i confused first,then i saw a comment saying its answer is 6 , then i again checked it, thats how i got it right.
byee
+1 vote
answered Jun 20, 2019 by Pirai (160 points)
6 is the answer
–1 vote
answered Jun 20, 2019 by half_deadlock
5

++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
++a=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
commented Oct 26, 2025 by Teja (110 points)
bro you are wrong too
i would agree with the first part
but in the second part ,it is post increment
so the value of a is used first and then the value of a is updated (i mean an increment is made)
SO
(++a) + (a++)=3 + 3
 now the value of a is 4
but the answer for that code is 6.
thank you for the oppurtunity to help you.
–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
0 votes
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 receive answers from other members of the community.
...