Determine output:
main()
{
int i = 5;
printf("%d%d%d%d%d", i++, i--, ++i, --i, i);
}
main()
{
int i = 5;
printf("%d%d%d%d%d", i++, i--, ++i, --i, i);
}
A. 54544
B. 45445
C. 54554
D. 45545
Answer: Option D
Solution(By Examveda Team)
The arguments in a function call are pushed into the stack from left to right. The evaluation is by popping out from the stack. and the evaluation is from right to left, hence the result. See the picture below
Join The Discussion
Comments ( 6 )
Related Questions on Function
In C, what is a function primarily used for?
A. Decision making
B. Variable declaration
C. Code organization and reusability
D. Printing output
What is the purpose of a function prototype in C?
A. To declare a function's return type
B. To declare a function's name
C. To declare a function's parameters and return type
D. To declare a function's name and parameters
In C, how are function arguments passed by default?
A. By value
B. By reference
C. By pointer
D. By address
int i = 7;
printf("%d %d", --i, i++)
First thing we will operate statement from right to left to get assigned value, i. e from i to i++
After operation we will put the assigned value from left to right
Second thing is in pre decrement/pre increment/variable doesn't have any pre/post fix, i.e in case off --i, ++i & i the value will assign in the end of the statement.
As we move towards right to left
First we take i, which has value 5
Assign value is expected to be 5 but it will remains empty because of 2nd rule, we will assign value in end
Our update value is still 5
Second we take --i, it is a pre decrement, means value will decrement first, then it will assign
So from our updated value, we have i=5
Decrement value is 5-1=4
Assign value is expected to be 4 but it remains empty because for second rule
Our updated value is now 4
Third we take ++i, it is pre increment means value will increment first, then it will assign.
So from our updated value, i=4
Increment value is 4+1=5
Assign value is expected to be 5 but it remains empty because of 2nd rule
Our update value is now 5
Fourth we take i--, it is post decrement means value will assign first, then it will decrement
From our updated value, we have i=5
Assign value is 5
Then decrement value is 5-1=4
So our new updated value is now 4
Lastly we have I++, post increment means value will assign first, then it will increment.
Our updated value is 4
Assign value is 4
Increment value is 4+1=5
Our new updated value is 5 and this is our final newest updated value
Now look for all 5 assign value
First assign value is empty
Second assign value is empty
Third assign value is empty
Fourth assign value is 5
Fifth assign value is 4
Now put our newest update value I, e 5 to empty assign value
So our value will
1st assign value 5
Second is 5
Third is 5
Fourth is 5
Fifth 4
And put assign value from left to right you get 45555
This answer is correct if you will execute in turbo c compiler
Bhai tera answer galat h, actually exact answer to tere option me v nahi h.
And the correct answer is 45555
Actually, when I execute this, I got output 45555. This is because every pre-increment and pre-decrement and 'i' values are replaced with final 'i' value.i.e,5
In the stack, why the first value is 4 instread of 5?