?
what will be the output when following code is executed?
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);
}
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
Step 2:
Declare
Step 3:
Evaluate
Step 4:
Evaluate
Step 5:
Sum the values obtained in steps 3 and 4: 10 + 12 equals 22.
Step 6:
Assign the sum (22) to
Step 7:
The
Step 8:
The first expression to be evaluated is
Step 9:
The next expression to be evaluated is
Step 10:
Finally, the last expression to be evaluated is
Therefore, the output of the program will be:
This output corresponds to the value of
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 14This output corresponds to the value of
b, followed by the evaluated values of a++, a, and ++a, respectively, in the printf statement.
Join the Discussion
Login to post a comment or share your explanation.
Login12(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
++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
b=(++a)+(++a)+(++a)-(--a);
printf("%d",b);
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
What are the values of a,b?
A)a=30,b=30
B)a=17,b=21
C)a=12,b=14
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)
b=(++a)+(a++)+(--a)
Give the value of b,a
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.
thanks @Anmol Bhatia for explaination
b = a++ + ++a; 10+12=22
output:
b =22
a++ = 12 after assign its incremented with 1
a = 13
++a = 14
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
please explain with each step...