Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > i++ * i++

Reply
Thread Tools

i++ * i++

 
 
rohit
Guest
Posts: n/a
 
      03-16-2006
#include <stdio.h>
#define PRODUCT(x) (x*x)

int main()
{

int i =3,j,k;
j = PRODUCT(i++);
k = PRODUCT(++i);

printf("\n%d %d", j,k);

return (0);
}

-----------------------------------------------------------
The output is 9 and 49

shouldn't the answer be 12 and 20?

What are the steps the compiler takes to reach the above given output?

 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      03-16-2006
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

rohit wrote:
> #include <stdio.h>
> #define PRODUCT(x) (x*x)
>
> int main()
> {
>
> int i =3,j,k;
> j = PRODUCT(i++);
> k = PRODUCT(++i);
>
> printf("\n%d %d", j,k);
>
> return (0);
> }
>
> -----------------------------------------------------------
> The output is 9 and 49
>
> shouldn't the answer be 12 and 20?


Nope. Because your code modifies an object twice between sequence
points, you've strayed into the realm of "undefined behaviour", and
/any/ answer is the "proper" answer.

> What are the steps the compiler takes to reach the above given output?


1) Flip Magic 8 Ball back and forth three times
2) Turn Magic 8 Ball face down
3) Read answer from Magic 8 Ball window

or not



- --

Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEGYuBagVFX4UWr64RAgNlAKDZqIXvfF7lVMFMrR2VP4 m68URgPQCgz7aN
dLkdF+Tl22Pt4hXN3e0yIyw=
=NM3n
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
 
Thomas Lumley
Guest
Posts: n/a
 
      03-16-2006

rohit wrote:
> #include <stdio.h>
> #define PRODUCT(x) (x*x)


This is asking for trouble: consider PRODUCT(2+2), which expands to
2+2*2+2,
ie 8 rather than 16, but that isn't what is causing this particular
problem.

> int main()
> {
>
> int i =3,j,k;
> j = PRODUCT(i++);


This is undefined behaviour: it expands to i++*i++

> k = PRODUCT(++i);
>
> printf("\n%d %d", j,k);
>
> return (0);
> }
>
> -----------------------------------------------------------
> The output is 9 and 49


Ok.

> shouldn't the answer be 12 and 20?


No. It can be anything. Yes, anything.

> What are the steps the compiler takes to reach the above given output?


Whatever it feels like.

Read the FAQ (starting with question 3.2, which is exactly this
question). (c-faq.com)
You might also find
http://www.eskimo.com/~scs/readings/undef.981105.html useful.

There is actually a perfectly sensible explanation for how the program
might have computed 9 and 49, but trying to second-guess undefined
behaviour is a bad idea.

-thomas

 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      03-16-2006
rohit <> wrote:
> #include <stdio.h>
> #define PRODUCT(x) (x*x)
>
> int main()
> {
>
> int i =3,j,k;
> j = PRODUCT(i++);
> k = PRODUCT(++i);
>
> printf("\n%d %d", j,k);
>
> return (0);
> }
>
> -----------------------------------------------------------
> The output is 9 and 49


For your compiler on your hardware platform, anyway.

> shouldn't the answer be 12 and 20?


The answer is that multiple assignments between sequence points yield
undefined behavior.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
Ben C
Guest
Posts: n/a
 
      03-16-2006
On 2006-03-16, rohit <> wrote:
> #include <stdio.h>
> #define PRODUCT(x) (x*x)
>
> int main()
> {
>
> int i =3,j,k;
> j = PRODUCT(i++);
> k = PRODUCT(++i);
>
> printf("\n%d %d", j,k);
>
> return (0);
> }
>
> -----------------------------------------------------------
> The output is 9 and 49
>
> shouldn't the answer be 12 and 20?
>
> What are the steps the compiler takes to reach the above given output?
>


See the FAQ, 3.1

http://c-faq.com/expr/index.html
 
Reply With Quote
 
Grumble
Guest
Posts: n/a
 
      03-16-2006
rohit wrote:
> #include <stdio.h>
> #define PRODUCT(x) (x*x)
>
> int main()
> {
> int i =3,j,k;
> j = PRODUCT(i++);
> k = PRODUCT(++i);
>
> printf("\n%d %d", j,k);
>
> return (0);
> }
>
> The output is 9 and 49
>
> shouldn't the answer be 12 and 20?


You'll want to read http://c-faq.com/expr/
 
Reply With Quote
 
Steve
Guest
Posts: n/a
 
      03-16-2006
When the compiler sees the i++ the value of i is not actually ever
incremented until after the current statement has executed. So when you
say i++ when i = 3 in that statement it is always 3 in that statement.
You increment the value twice since it says i++ * i++ only in the next
statement. With the ++i the value is incremented before the statement
starts so it sees a 5 from the above line then increments twice to 7 and
then preforms the PRODUCT.

rohit wrote:
> #include <stdio.h>
> #define PRODUCT(x) (x*x)
>
> int main()
> {
>
> int i =3,j,k;
> j = PRODUCT(i++);
> k = PRODUCT(++i);
>
> printf("\n%d %d", j,k);
>
> return (0);
> }
>
> -----------------------------------------------------------
> The output is 9 and 49
>
> shouldn't the answer be 12 and 20?
>
> What are the steps the compiler takes to reach the above given output?
>

 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      03-16-2006
Steve <> wrote:

[ Do not top-post, please. ]

> > #define PRODUCT(x) (x*x)


> > j = PRODUCT(i++);
> > k = PRODUCT(++i);


> > The output is 9 and 49
> >
> > shouldn't the answer be 12 and 20?


> When the compiler sees the i++ the value of i is not actually ever
> incremented until after the current statement has executed.


This is wrong. What happens when you execute i++ is this:
- the value returned is the value of i before this expression, _and_
- i is incremented.
In which order these are done is not defined; they could even be done in
parallel.

> So when you
> say i++ when i = 3 in that statement it is always 3 in that statement.


This, too, is wrong.

Richard
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      03-16-2006
rohit wrote:

> #include <stdio.h>
> #define PRODUCT(x) (x*x)


Should be ((x)*(x)) - consider the argument `i + 1`.

Actually, more like "should be `int product(x) { return x*x; }`.

> int main()
> {
>
> int i =3,j,k;
> j = PRODUCT(i++);


BOOM.

> k = PRODUCT(++i);
>
> printf("\n%d %d", j,k);
>
> return (0);
> }
>
> -----------------------------------------------------------
> The output is 9 and 49
>
> shouldn't the answer be 12 and 20?


This a FAQ, so see the C FAQ, eg

http://www.faqs.org/faqs/C-faq/faq/
question 3.2

> What are the steps the compiler takes to reach the above given output?


It compiles code for `i++ * i++` assuming that you know what you're
doing. So, at a guess, it multiples i (3) by i (still 3) to get 9.
The it increments i, possibly twice.

The it compiles code for `++i * ++i` ATYKWYD. At a guess, it increments
i, probably twice, then multiplies i (7) by i (still 7) to get 49.

The compiler-writer of course could do things differently. They could
keep a flag for each variable, "increment pending". Then `i++` compiles
as "get the value of i and set the flag", and ";" compiles as "increment
all variables with the flag set, and clear it"; and "++i" compiles as
"increment i, and return the new value". In that case, you'd
get the results 9 and 20.

Or they could compile `i++` as "if the flag is set, report a problem,
plant code to quit the program; otherwise, get the value of i and
set the flag."

--
Chris "sparqling" Dollin
"Who do you serve, and who do you trust?"
 
Reply With Quote
 
Steve
Guest
Posts: n/a
 
      03-16-2006
This is short sighted because of a few other reasons but wrong is
extremely, just another way to interpret the actions the final result.
Thanks though for pointing it out.

Richard Bos wrote:
> Steve <> wrote:
>
> [ Do not top-post, please. ]
>
>>> #define PRODUCT(x) (x*x)

>
>>> j = PRODUCT(i++);
>>> k = PRODUCT(++i);

>
>>> The output is 9 and 49
>>>
>>> shouldn't the answer be 12 and 20?

>
>> When the compiler sees the i++ the value of i is not actually ever
>> incremented until after the current statement has executed.

>
> This is wrong. What happens when you execute i++ is this:
> - the value returned is the value of i before this expression, _and_
> - i is incremented.
> In which order these are done is not defined; they could even be done in
> parallel.
>
>> So when you
>> say i++ when i = 3 in that statement it is always 3 in that statement.

>
> This, too, is wrong.
>
> Richard

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57