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

+9 votes
asked Jun 18, 2019 by kj

37 Answers

0 votes
answered Feb 3 by Mahesh Phutane (140 points)

answer is 6, as it follow right to left precedence.

    4   +    2  =  6

(++a + a++)

   4        3

0 votes
answered Feb 3 by anonymous
5 wil be the answer because ++a indiactes pre increment operator which increments value before execution.

a++ is post increment operator which will increment the value after execution.
0 votes
answered Feb 3 by Deep
7

because:initially a=2; ++a=3 then next a++ =4. Thus, ++a + a++ = 3+4=7
0 votes
answered Feb 5 by kothakushal (560 points)
7 because for the ++a the value becomes 3 and again a++ the value becomes 4

so,finally 3+4=7
0 votes
answered Feb 5 by Rajeshkumar
a=2

++a=3

a++=2

++a + a++ = 5
0 votes
answered Feb 14 by anonymous
6 is the right answer
0 votes
answered Mar 25 by Alan Sampson (380 points)

Anyone who provided a value as an answer is wrong! This is "undefined behavior" because it's a sequence error. You can't update a variable more than once inside the same sequence point. To see the proof, add the following compiler switches: -Wall -Werror and recompile the program. The compiler issues the following error message:

main.c:15:27: error: operation on ‘a’ may be undefined [-Werror=sequence-point]
   int a=2; printf("%d\n", ++a + a++);
                           ^~~
cc1: all warnings being treated as errors

 

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