int i=5; p=(++i)*(++i)*(++i);should p always be 392?

L

Liu Jin

392 means 7*7*8
Can't it be 8*8*8 or any other?
I wonder whether the calculation sequence in such an expression is
specified in ansi c.
Thanks in advance.
 
Z

Zoran Cutura

Liu Jin said:
392 means 7*7*8
Can't it be 8*8*8 or any other?
I wonder whether the calculation sequence in such an expression is
specified in ansi c.
Thanks in advance.

It is not defined, and this code invokes so called undefined behavior
which means that anything may happen, from nuclear weapon attack at your
desk or nasal daemons to program runs as expected by silly programmer.

You must not modify an object more than once inbetween to sequence
points.

Please read the faq: http://www.eskimo.com/~scs/C-faq/s3.html
 
A

Arthur J. O'Dwyer

392 means 7*7*8

That's one way of putting it, although (1) that assumes you're
writing in base 10; and (2) the factorization 2^3*7^2 is a much more
traditional way of representing 7*7*8.
Can't it be 8*8*8 or any other?

8*8*8 is 2^9, or 512 base 10. That's not 392 in any base.
Sorry. (Unless you're using a different meaning for *
than multiplication, of course.) It's just the way that
Western mathematical notation works.
I wonder whether the calculation sequence in such an expression is
specified in ansi c.

What sort of expression? 7*7*8? In that case, it really doesn't matter
which operation is done first. (ISO C does specify that most operations
of the same precedence take place from left to right, though.)
Thanks in advance.

Try sci.math for questions about prime factorization.

-Arthur
 
B

bd

Subject: int i=5; p=(++i)*(++i)*(++i);should p always be 392?
392 means 7*7*8
Can't it be 8*8*8 or any other?
I wonder whether the calculation sequence in such an expression is
specified in ansi c.

Nothing is specified about the (poorly) indicated expression - your
compiler can do whatever it likes with it, which is not restricted to
merely modifying i and p. In theory, it could make demons fly out of your
nose.
 

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,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top