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 Jin

    Liu Jin Guest

    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.
    Liu Jin, Jul 9, 2003
    #1
    1. Advertising

  2. Liu Jin

    Zoran Cutura Guest

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

    --
    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
    #2
    1. Advertising

  3. 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
    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
    Arthur J. O'Dwyer, Jul 9, 2003
    #3
  4. Liu Jin

    bd Guest

    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
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,211
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,634
    Old Wolf
    Jan 20, 2004
  3. arun
    Replies:
    8
    Views:
    453
    Dave Thompson
    Jul 31, 2006
  4. Replies:
    6
    Views:
    6,398
    Greg Comeau
    Oct 19, 2005
  5. aling
    Replies:
    8
    Views:
    947
    Jim Langston
    Oct 20, 2005
Loading...

Share This Page