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

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.

Ashish said:   1 decade ago
step 1: i-3;
step 2: i= i; than i++;
step 3: i++ => i=i+1;
so i=4;

Anjena said:   1 decade ago
Then what abt i-- ? Anyone can clear my doubt?

i=5, j=7;

printf("%d %d %d %d", ++i, ++j, i++, j++);

Souvik said:   1 decade ago
The output will vary compiler to compiler,because i++means,i=i+1;
so when u r assign i=i++ then ambiguity may come, so depending upon the compiler o/p changes.

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

Mandy said:   1 decade ago
Thank Abiyot.

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!

Sundar said:   1 decade ago
@Karthikeyan You are absolutely correct.

Let me give my points too.

i = i++; // You may have little confusion here.

The above statement will be executed as given below:

Step-1: i = i; // Here i = 3
Step-2: i = i+1; // Here i = 3 + 1 = 4.

Therefore, 4 is the correct answer.

Karthikeyan said:   1 decade ago
Step 1: int i=3
Step 2: i=i++ post increment so still i=3 in this statement
Step 3: after completion of the above statement i will be increment by 1 so i becomes 4
step 4: in printf statement i value is printed i.e:4

Subhajyoti said:   1 decade ago
i is initialised a value 3,which is again assigned to i as the post decrement operator will be executed after completion of the statement.after completion of the statement i value increases to 4 which will be the output.


Post your comments here:

Your comments will be displayed after verification.