ExamVeda
Login
Home
Operators and Expressions
?

what will be the output when following code is executed?
void main()
{
   int a=10, b;
   b = a++ + ++a;
   printf("%d %d %d %d", b, a++, a, ++a);
}

Answer & Solution
Correct Answer: Option E
Step 1:
Initialize a with the value 10.

Step 2:
Declare b without initialization (the initial value is undefined).

Step 3:
Evaluate a++ (post-increment):
  - The current value of a is 10.
  - After evaluating a++, a becomes 11.

Step 4:
Evaluate ++a (pre-increment):
  - ++a increments a to 12 and returns the updated value, which is 12.

Step 5:
Sum the values obtained in steps 3 and 4: 10 + 12 equals 22.

Step 6:
Assign the sum (22) to b.

Step 7:
The printf function follows the stack Last-In-First-Out (LIFO) concept, which means that the expressions are evaluated from right to left.

Step 8:
The first expression to be evaluated is ++a:
  - ++a increments a to 13 and returns the updated value, which is 13.

Step 9:
The next expression to be evaluated is a++:
  - a++ returns the current value of a (13) and then increments a to 14.
  - The returned value is 13.

Step 10:
Finally, the last expression to be evaluated is a, which has the current value of 14.

Therefore, the output of the program will be:
22 13 14 14

This output corresponds to the value of b, followed by the evaluated values of a++, a, and ++a, respectively, in the printf statement.
Examveda
Question posted by Examveda
Community

Join the Discussion

26 Comments
Subhankar Lenka
Subhankar Lenka 3 years ago
b = 11+11=22
12(new value of a)
* printf exicution from right to left and printing from left to right
so ++a=13
a=13
a++= 1st 13 printed then increment i.e. a=14
so ans = 22 13 13 13
Gunvatta
Gunvatta 4 years ago
b = 10++ =11
++11 =12(new value of a)
i.e. b = 10+12 =22
a++ = 12 (first use then incremented )
a = 13 incremented.
++a = 14 (first incremented then used)

Hence = 22 12 13 14

Gunvatta
Gunvatta 4 years ago
The correct ans is 22 12 13 14 which is not in options
CSE_58_RAJ KUMAR
CSE_58_RAJ KUMAR 5 years ago
a=10;
b=(++a)+(++a)+(++a)-(--a);
printf("%d",b);
Narendra B
Narendra B 6 years ago
A) a=30 b=30
Narendra B
Narendra B 6 years ago
A) a=30 b=30
YOKESWARAN S
YOKESWARAN S 6 years ago
logic says 22,13,13,13
b=10+12=22
then printf ( b, a++, a, ++a); associativity left to right
++a =13 bcz it is preincrement
then a is same as before value a= 13
after that post increment a= 13 will be
next we got above b=22
therefore answer is 22 13 13 13
Potta Yegnajayasimha
Potta Yegnajayasimha 6 years ago
a=5,b=a+=10 + ++ a + ++ a;
What are the values of a,b?
A)a=30,b=30
B)a=17,b=21
C)a=12,b=14
Juniper
Juniper 6 years ago
The answer is option E.
b will get evaluated as:-
a++ (post increment), so its 10 (initially), then it becomes 11.
11 is received by ++a, so it pre increments it and becomes 12.
so b=10+12=22.
Now, printf() follows the stack LIFO concept. So, the expression at the end is first evaluated, followed by the second last and so on. Here, The last expression given is ++a, This is pre increment. Which means, that the increment will be done first followed by the assignment. So, a=13. SO it gets incremented first, but not yet assigned. Then comes a. this also will be assigned later. Till now, a is 13, but not assigned yet. Thereafter comes a++. It is post increment so it assigns first, increments later. So, the assigned value is 13. So now, in place of a++, 13 will come. After assigning, it gets incremented to 14. b is already 22. So, now final assignment will begin. b will be assigned 22, a will be assigned 14, and ++a will have the value 14( as the pre increment was already done, it wont be done again)
(NOTE: This process vary depending on compiler to compiler. This is strictly as per GCC)
So, the answer will be 22 13 14 14 (OPTION E)
Jitendra Sahoo
Jitendra Sahoo 7 years ago
Answer is 22 13 14 14
Desh Deepak
Desh Deepak 8 years ago
Int a=14,b;
b=(++a)+(a++)+(--a)
Give the value of b,a
Deepanshi Chauhan
Deepanshi Chauhan 8 years ago
logic says 22 13 13 13, but compiler says 22 13 14 14
Rohan Shingan
Rohan Shingan 8 years ago
how??
Ravindra Mulane
Ravindra Mulane 8 years ago
Ans: 22 12 14 14
As precedence value of post increment is higher than pre increment.
Hence, first a++ will be executed then ++a and then a.
Try, Compile the program to find right answer.
Please team exam veda update right option.
MITTU SHARMA
MITTU SHARMA 8 years ago
ok its right 22 13 13 13
thanks @Anmol Bhatia for explaination
MITTU SHARMA
MITTU SHARMA 8 years ago
its correct answer is 22 12 13 14
b = a++ + ++a; 10+12=22
output:
b =22
a++ = 12 after assign its incremented with 1
a = 13
++a = 14
Shraddha Baviskar
Shraddha Baviskar 8 years ago
but in code bloks its output 22 13 14 14 why?
Anmol Bhatia
Anmol Bhatia 9 years ago
b=a++ + ++a
b=10+12=22
a=12
printf is first scanned from right to left
++a=13
a=13
a++=13
now it will print
b and then all the a values which in dis case are all 13
so ans is
22 13 13 13
Saurabh Jare
Saurabh Jare 9 years ago
22 13 13 13
Umang Mahant
Umang Mahant 9 years ago
thanks... Varsha Pandey
Varsha Pandey
Varsha Pandey 9 years ago
22 13 13 13 is correct because in printf in execute from right to let so 1st ++a=13, a=13,a++=13,b=22 and one cycle of a++ is remaining so that will not be executed because there is no further a given.
D&D
D&D 10 years ago
Answer would be 22 12 13 14
KARAN PANDEY
KARAN PANDEY 10 years ago
22 13 14 14
Shailendra Raghuvanshi
Shailendra Raghuvanshi 10 years ago
Ans is b
Darshan Bodige
Darshan Bodige 10 years ago
Couldn't understand the soln plzz elaborate
Arun Kumar
Arun Kumar 10 years ago
i cant understand clearly...
please explain with each step...