I know its silly but once i tried this and output is not what it should be..

#include<stdio.h>
#include<conio.h>
int main()
{
int i=0;
i=i++;
printf(i);
}

i MEAN post increment will store a value, say 0 here.and then increment but that value is given to the variable i again by the assignment operator which is printed .the ouput is 1 but it should be 0.

Recommended Answers

All 9 Replies

Edit:: Don't bother reading this.

Writing i=i++; is essentially the same as writing:

i=i;
i++;

and because it's the same variable here it's essentially the same as writing: i++;

No man, I DON'T THINK SO...
We cant write i=i++
as i =i; or can we????( in this case 0 will be printed on screen)
or as i++ ( i agree 1 shud be printed)
but still my question for the assignment that happens last..the value 0 shud go to the variable i in i=i++;and it should print 0 instead of 1...

Edit:: Don't bother reading this.

No man, I DON'T THINK SO...
We cant write i=i++
as i =i; or can we????( in this case 0 will be printed on screen)
or as i++ ( i agree 1 shud be printed)
but still my question for the assignment that happens last..the value 0 shud go to the variable i in i=i++;and it should print 0 instead of 1...

>We cant write i=i++ as i =i;
I didn't say that, can you quote me the sentence?

I guess you didn't understand me, let's analyze it:
If you write i=i++; , then it first assigns the value of variable i to variable i, at this point the value of i is still zero (Step 1).
At the end of the expression, the post increment operator (++) increments the value of variable i with one (Step 2).
That's what I meant with saying that writing i=i++; is essentially the same as writing:

i=i; // ([I]Step 1[/I])
i++; // ([I]Step 2[/I])

But assignment should occur as a last step..isntd????
and then why does this code in java prints 1?????
i 'm sorry i mentioned java in c forum..but here's my doubt!!
Because assignment operator has the lowest priority..

>and then why does this code in java prints 1?????
i 'm sorry i mentioned java in c forum..but here's my doubt!!

I think it's better to ask this in the Java forum.

According to your doubt: i=i++; , as the link in Dave Sinkula's post mentions:
it's an undefined expression, so the result can differ from implementation to implementation.
For example, on my and on your compiler your program gave the same output, but it isn't guaranteed that this program will produce the same output on every implementation.

Hi,
I am new to this forum and am pretty young (12).
However, I can still give my piece of advise.
First of all as all my other friends replied:

i=i++;

is same as

i++;

Further more you missed out something which I have pointed in bold.

#include<stdio.h>
#include<conio.h>

int main()
{
int i=0;
i=i++;
printf[B]("%d", [/B]i);
}

Dont know whether writing "%d" is imp. but I think so.

commented: Check the date, please. +0

you want the result to be 0 or 1?

your code gives the result as 0 (using C)

i=i++; is wrong for increment of i

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.