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);
}
A. 12 10 11 13
B. 22 12 12 13
C. 22 11 11 11
D. 22 14 12 13
E. 22 13 14 14
Answer: Option E
Solution(By Examveda Team)
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.
Join The Discussion
Comments ( 26 )
Related Questions on Operators and Expressions
What does the ++ operator do in C when applied to a variable?
A. Decrements by 1
B. Adds 1
C. Doubles the value
D. Leaves it unchanged
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
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
The correct ans is 22 12 13 14 which is not in options
a=10;
b=(++a)+(++a)+(++a)-(--a);
printf("%d",b);
A) a=30 b=30
A) a=30 b=30
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
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
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)
Answer is 22 13 14 14
Int a=14,b;
b=(++a)+(a++)+(--a)
Give the value of b,a
logic says 22 13 13 13, but compiler says 22 13 14 14
how??
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.
ok its right 22 13 13 13
thanks @Anmol Bhatia for explaination
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
but in code bloks its output 22 13 14 14 why?
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
22 13 13 13
thanks... Varsha Pandey
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.
Answer would be 22 12 13 14
22 13 14 14
Ans is b
Couldn't understand the soln plzz elaborate
i cant understand clearly...
please explain with each step...