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

Discussion in 'C Programming' started by Liu Jin, Jul 9, 2003.

1. ### Liu JinGuest

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.

Liu Jin, Jul 9, 2003

2. ### Zoran CuturaGuest

Liu Jin <> wrote:
> 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.

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.

--
Z ()
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond

Zoran Cutura, Jul 9, 2003

3. ### Arthur J. O'DwyerGuest

Re: int i=5; blah blah; - )

On Wed, 9 Jul 2003, Liu Jin wrote:
>
> 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

> 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.)

Try sci.math for questions about prime factorization.

-Arthur

Arthur J. O'Dwyer, Jul 9, 2003
4. ### bdGuest

On Wed, 09 Jul 2003 02:46:51 -0700, Liu Jin wrote:

> 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.
--
Freenet distribution not available
The rule is, jam to-morrow and jam yesterday, but never jam today.
-- Lewis Carroll

bd, Jul 9, 2003