ExamVeda
Login
Home
This question belongs to C Program Function
Function
?

Determine output:
main()
{
      int i = 5;
      printf("%d%d%d%d%d", i++, i--, ++i, --i, i);
}

Answer & Solution
Correct Answer: Option D

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
Function mcq solution image

Examveda
Question posted by Examveda
Community

Join the Discussion

6 Comments
Mounota Hossain
Mounota Hossain 4 years ago
int i = 7;
printf("%d %d", --i, i++)
Soumya Saswati
Soumya Saswati 5 years ago
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




Surendra Kumar
Surendra Kumar 5 years ago
This answer is correct if you will execute in turbo c compiler
Dipak Kumar
Dipak Kumar 9 years ago
Bhai tera answer galat h, actually exact answer to tere option me v nahi h.
And the correct answer is 45555
ANUSHA Pantham
ANUSHA Pantham 9 years ago
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
Sajid Khan
Sajid Khan 9 years ago
In the stack, why the first value is 4 instread of 5?