Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   incremental multiplication problem (http://www.go4expert.com/forums/incremental-multiplication-problem-t23943/)

crazyNut 21Nov2010 12:06

incremental multiplication problem
 
Hi everyone,

I am a beginner of C and am stuck on a problem like below

int i=3, j, k;
j = i++*i++;
k = ++i*++i;
print ("j=%d k=%d", j, k);

As per my calculations the values should be j=12 and k=42 but the value of the code turns out to be j=9 and k=49. I don't understand why it happens. Can anyone explain the reason.

Thanks for any Help

ihatec 21Nov2010 19:29

Re: incremental multiplication problem
 
Just play a bit with operators and you will find out how to use to obtain correct result.

jimblumberg 21Nov2010 19:58

Re: incremental multiplication problem
 
This link should help to explain the results. Sequence_point.

Jim

crazyNut 21Nov2010 20:22

Re: incremental multiplication problem
 
Quote:

Originally Posted by ihatec (Post 75022)
Just play a bit with operators and you will find out how to use to obtain correct result.

Is it something like in case of i++
the compiler first evaluates the expression
j = i++*i++
and then increments i 2 times in a row
and opposite for ++i
means there it first increments i 2 times in a row and then evaluates the exp k = ++i*++i


Thanks

crazyNut 21Nov2010 20:27

Re: incremental multiplication problem
 
Quote:

Originally Posted by jimblumberg (Post 75024)
This link should help to explain the results. Sequence_pionts

Jim

The link was very useful.
It contains some good points.

Thanks

xpi0t0s 22Nov2010 13:45

Re: incremental multiplication problem
 
Quote:

Originally Posted by crazyNut (Post 75028)
Is it something like in case of i++
the compiler first evaluates the expression
j = i++*i++
and then increments i 2 times in a row
and opposite for ++i
means there it first increments i 2 times in a row and then evaluates the exp k = ++i*++i
Thanks

No. Look again at the definition of a sequence point. It is a point after which all side effects are guaranteed to have been completed. It is *NOT* a point before which all side effects are guaranteed NOT to have been completed. Before a sequence point, some side effects may or may not have been completed and you have no way of knowing which have and which haven't.

So i=3; j=i++ * i++;
could mean j=i(3, post incremented to 4) * i(4, post incremented to 5)=12,
OR it could mean j=i*i(post incremented twice to 5)=9.
The result is compiler dependent. In Visual Studio j=i++*i++; is equivalent to j=i*i; i++; i++; but you cannot guarantee this for all compilers (or even all versions of Visual Studio).

The end result of this is that if you only need a variable once in an expression, it's safe to use modifiers, but if you need it more than once, use it WITHOUT pre/post increment/decrement within the expression, THEN modify it.

By the way, this is a very frequently asked question. Check out stuff like the C++ FAQ before asking anything else. It will almost certainly answer your next 20 questions, plus there will be a load of other interesting stuff in there, so it's well worth a read.


All times are GMT +5.5. The time now is 02:41.