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++);

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

38 Answers

–1 vote
answered Jun 27, 2019 by priyanka (120 points)
–1 vote
answered Jun 27, 2019 by anonymous

I tested in the online C compiler.
The result printed was 7.

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

The explanation is that 

 ++a:    uses the incremented value of "a" AND increments the value of "a" in one step

a++:  uses the incremented value of "a" .   After the expression is evaluated and assigned, it THEN increments a

So in order of what happens
"++a"    "+"   "a++"

a starts at 2

the expression increments a and sets a to that increment. so the first term "++a" has been evaluated to 3, also  a=3 now
3   "+"   "a++"

now we plug 3 into a

3 "+" "3++" which is
3 "+" "4" 
since that is the end of the expression, it returns the value 3+4 = 7, to the print function.

then it changes a to 4. (by incrementation)

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

would result in 11 at the end.

the difference is after a++, there is more expression(s) to be evaluated.

so before it goes to the next expression, it sets a to 4.

so we have the 7 from before, and its added by an a.  and since the a = 4, we have 7+4 = 11


–1 vote
answered Jun 28, 2019 by anonymous
the ans is 6
–1 vote
answered Jun 29, 2019 by anonymous
answer will be 6.
–1 vote
answered Jun 30, 2019 by anonymous
finally answer is 6
–1 vote
answered Jul 1, 2019 by anonymous
in the question operation is executed from left to right

i.e; 1) ++a is executed (++a=3)

       2) a++ is executed ,here increment is done but not stored so (a++=3)

       3) addition is done 3+3=6

–1 vote
answered Jul 1, 2019 by Srivasthava (120 points)

Answer is 7

At first preincriment operation is done then value of a is 3. now the value of A is 3 . when postincriment operation is performed then its value is 4.Finally sum of these two is 7. 

–1 vote
answered Jul 2, 2019 by pallavi
correct answer: 6
–1 vote
answered Jul 2, 2019 by MANI KANTH (120 points)
++a=3 a++ remains 3 only. So ++a+a++=6
–1 vote
answered Jul 3, 2019 by anonymous
Correct answer is = 5

++a it will not take any increment.

a++ is equals to a+1.

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.