In article <news:63*************************@posting.google.c om>
Peter Nilsson <ai***@acay.com.au> writes:
[regarding the grammar in the C Standard and a certain footnote]
What's the difference between 'binds' and precedence?
In computer-language-parsing jargon, none. In compiler work
"binding" is a more general term; one might refer to "early" vs
"late" "binding" of names in linkage, for instance. In ordinary
conversational English, however, the word "bind" avoids the
connotation that "precedence" gives. Specifically, by talking
about how some operator might "bind left to right", we can avoid
having the reader see this as "subsequently proceed to operate in
sequence at runtime".
The phrase "operator precedence" is literally made up of the words
"operator precede" plus a few extra letters ("nce"), from which
one might assume that if "a + b * c" gives the multiplication
operator "precedence", then the entire "b * c" part will "precede"
any and all of the "a +" part. But in fact, if one runs the
following C code:
#include <stdio.h>
int f(void), g(void), h(void);
int main(void) {
printf("result = %d\n", f() + g() * h());
return 0;
}
int f(void) { puts("f"); return 3; }
int g(void) { puts("g"); return 4; }
int h(void) { puts("h"); return 5; }
the only *requirements* are that the output contain the three lines
"f", "g", and "h" (in any order) followed by "result = 23". The
"g" and "h" lines need not come out first -- f() can run first,
for instance, even though the addition at least appears to happen
last -- and in fact, using gcc on the x86, I get:
% cc -O -o u u.c -W -Wall -ansi -pedantic
% ./u
f
g
h
result = 23
Adding parentheses around g() * h() will usually have no effect
(not really surprising, since the multiplication operator is already
bound to the result of the two function calls, and parentheses need
only affect the parse binding). The only way to *force* g() and
h() to run before f() is to use C's runtime sequencing tools, called
"sequence points". For instance:
int t1, t2;
t1 = g();
t2 = h();
t1 *= t2;
t1 += f();
(and the corresponding change to the "result =" line) will force
the output to be "g\nh\nf\nresult = 23\n".
The complete list of sequence points can be found in the C standard,
and a summary in the FAQ.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it
http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.