small problem !!!

R

Rahul

hi ,
i cant understand whats wrong with the following piece of code:-

main()
{
..
a=i++;
..
..
}

Well it works fine with my compiler. The old value of i is assigned to
a and then it gets incremented.
Can any one please help me out?
thanks.
 
C

Clark Cox

hi ,
i cant understand whats wrong with the following piece of code:-

main()
{
.
a=i++;
.
.
}

Well it works fine with my compiler. The old value of i is assigned to
a and then it gets incremented.


It just happens to work fine on your platform. You have no guarantee
whether the new value gets stored to i before or after the location of
a is calculated. They could even happen simultaneously.

Just don't do it.
 
K

Keith Thompson

Clark Cox said:
i cant understand whats wrong with the following piece of code:-
main()
{
.
a=i++;
.
.
}
Well it works fine with my compiler. The old value of i is assigned
to
a and then it gets incremented.


It just happens to work fine on your platform. You have no guarantee
whether the new value gets stored to i before or after the location of
a is calculated. They could even happen simultaneously.


It's worse than that. The statement invokes undefined behavior (as
the FAQ explains; see 3.1 and 3.9).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top