C Programming - Expressions - Discussion

Discussion Forum : Expressions - Find Output of Program (Q.No. 9)
9.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int i=3;
    i = i++;
    printf("%d\n", i);
    return 0;
}
3
4
5
6
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
70 comments Page 3 of 7.

Mohammad Mohsin said:   10 years ago
Answer must be 3, because post increment operator assigns value first and then incremented.

In DEV CPP while I run above program answer is 3.

Shashi said:   1 decade ago
@All above are wrong : Works from right to left.

The statement i = i++ means i == i + 1;.

Step 1: i = i + 1 results i = 4.

Step 2: i = 4.

Nagamani said:   6 years ago
No, the actual answer is 3 because it is the dependent expression the value of I will assigned to the left side and than value incremented.

Gaurav said:   8 years ago
@Rishabh.

You're damn right! the answer is 3 in case of such post increment.

i = i++;
is analogous to
temp = i;
i = i + 1;
i = temp;

Prashant said:   7 years ago
It is wrong because of i=3;

i=i++;
first i=4;
seound i++=3;
than
i=i++;

So, i++ override the value of i;
The output will be 3;.

Coolant said:   4 years ago
Answer will be 3.

i=i++
i=3++
I take the value 3 there won't be an increment.
Priority will be given to i=3 not to i++.

Vrushabh said:   8 years ago
Isn't it depend on the compiler. I mean in c++,
int x = 10;
x = ++x;
cout<<x;
it will still give 10 as output.

Riddhi Mitkari said:   2 years ago
Correct answer is 4.
because i=3
i=i++ // this is post increment so i=3 but internally i is increment by 1.
so i=4.
(2)

Abiyot Hordofa said:   1 decade ago
I am very appreciate you to give me such like chance to see myself how I understand the course!!!!
Thanks to you!

Punit Sharma said:   9 years ago
All of you are wrong. The post-increment effect is damaged by the assignment operator. Thus the value of i=3.


Post your comments here:

Your comments will be displayed after verification.