lkml.org 
[lkml]   [1998]   [May]   [19]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: Egcs 1.0.3 & Linux
On Tue, 19 May 1998, Stephen Williams wrote:

> > #include <stdio.h>
> > int a=0;
> > int f(void) { a=1;return 1; }
> > void main(void) { printf("%d",f()+a); }
>
> chris@cybernet.co.nz said:
> > I was under the impression that in the case of equal operator
> > precedence the order was defined to be strictly left to right, so in
> > this case it should return 2 and never 1.
>
> There is nothing in the standard that says a must be evaluated before
> b in (a+b). What it says is that (a+b) must be evaluated before a+b+c
> can be evaluated. It is even legitimate in the latter case for c to be
> evaluated before a, but it cannot be added to a, it must be added to (a+b).
>
> Right?
>

The defining rule(s):
"The result of an operation that modifies an object more than once,
without an intervening sequence-point, is undefined."

Like: a = a++; (this could erase your hard-drive)

"A function returning a value shall be evaluated first. Multiple
functions returning values are evaluated left-to-right".

Like: a = b() + c() + d() + 1;
==
a = 1 + b() + c() + d();

The sequence-point here, is the operation that must be completed before
the call to printf().

Like: printf("%d", ((f()) + a);
|_________|___ sequence-point.

'a' was modified once by a call to f(). The result was added to 'a' before
the call to printf(). The code should therefore print '2'. If it doesn't,
the compiler is broken.

If the code were written as:
a = f() + a;
|_______ sequence-point
'a' would be modified twice before ';'.

The result is undefined so the compiler can do anything it wants including
easing your disk drive or raiding your savings account.

Of course one would not write code like this, er, I hope not??

Cheers,
Dick Johnson
***** FILE SYSTEM MODIFIED *****
Penguin : Linux version 2.1.101 on an i586 machine (66.15 BogoMips).
Warning : It's hard to remain at the trailing edge of technology.


-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu

\
 
 \ /
  Last update: 2005-03-22 13:42    [W:0.078 / U:0.056 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site