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.

Edwin Jose said:   1 decade ago
In post increment i value get incremented in the next step only.

Arnab Sanyal (IIT, BOMBAY) said:   1 decade ago
i is initialised to 3. We know that every variable can hold one value at a time. So only after assigning 3, with post increment operator i is assigned to 4.

Srini 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 as 4

Meghana said:   1 decade ago
@Anjena the answer will be:.

7 9 5 7 (evaluate from right to left).

Himanshu said:   1 decade ago
Actually precedence of post increment operator (++) is greater than = operator.

So, i=i++; first increment occurs and then assignment occurs.

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.


Post your comments here:

Your comments will be displayed after verification.