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 5 of 7.

Rishabh said:   8 years ago
I think the answer is 3. I run it on both GCC complier or turbo C compiler try it on your own.

Karthik said:   9 years ago
I tried this code in code block and I got the output as 3. But here, the compiler says 4. Why?

PRASHANT MARATHE said:   5 years ago
Post increment -- after assigning the value to the variable the value is incremented. i.e 3.

Sandy said:   8 years ago
@ALL.

int x=3;
i=i++; which means i=i+1; => i=4;
printf("%d",i); prints 4.

Beena said:   1 decade ago
Here the value of i is post incremented in the same memory space of i so the value 4.

Rahul Agrawal said:   9 years ago
Always go with the last operation here i++ is the last operation so the answer is 4.

Dixit said:   1 decade ago
First i = 3 will be assigned to i (i = i++).

Then i will be incremented to 4.

Juber said:   10 years ago
int i = 3;
i = 3+3;
i = 6;
i++;
i = 7;
i++;
i = 8;

Finally i = 8 will be print;

Meghna Anam said:   6 years ago
I have compiled this program and I get output 3.

So, the correct answer is 3.

Tayo said:   8 years ago
I just ran it on my computer and it is 3 not 4. It's a post-increment.


Post your comments here:

Your comments will be displayed after verification.