Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > int i=5; p=(++i)*(++i)*(++i);should p always be 392?

Reply
Thread Tools

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

 
 
Liu Jin
Guest
Posts: n/a
 
      07-09-2003
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.
 
Reply With Quote
 
 
 
 
Zoran Cutura
Guest
Posts: n/a
 
      07-09-2003
Liu Jin <(E-Mail Removed)> 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/%7Escs/C-faq/s3.html

--
Z ((E-Mail Removed))
"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
 
Reply With Quote
 
 
 
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      07-09-2003

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

 
Reply With Quote
 
bd
Guest
Posts: n/a
 
      07-09-2003
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

 
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
why is int a[0] not allowed, but int* a = new int[0] is? haijin.biz@gmail.com C++ 9 04-17-2007 09:01 AM
Difference between int i, j; and int i; int j; arun C Programming 8 07-31-2006 05:11 AM
int a[10]; int* p=(int*)((&a)+1); But why p isn't equal to ((&a)+1)? aling C++ 8 10-20-2005 02:42 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments