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.

Karthik said:   9 years ago
I tired this;

#include<stdio.h>
int main()
{
int i=3;
i = i++;
printf("%d\n%d", i,i++);
return 0;
}

______________
The output is
3
4

Can anyone explain how i++ became 3?

Sweety said:   9 years ago
Answer will be 3, not 4. Just compile it on another online compiler. And conceptually also it should be 3.

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.

Akanksha said:   10 years ago
Can anyone solve my doubt? What is the output of this?

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

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.

Saurabh said:   1 decade ago
For that question some compiler will give different answer like Dev C++.

Is giving 3 instead of 4 because when a single expression causes the same object to be modified then inspected the behavior is undefined. In this case I = i++;

Here variable I is being modified and then it is being used by the compiler. That's why answer 4 is coming here.

Sneha naik said:   1 decade ago
@Suraj.

Answers will be 4, 3 in post increment value will be assigned later incremented so j=3 and i=4.

Krishanu said:   1 decade ago
Please run that code the out will be 3.

Amolak said:   1 decade ago
@Siraj.

Output will be : 3, 4.

Siraj said:   1 decade ago
#include<stdio.h>
int main()
{
int i=3,j;
j = i++;
printf("%d %d\n", i,j);
return 0;
}

What will be the output now?


Post your comments here:

Your comments will be displayed after verification.