Order of evaluation -- clash w/sequentiel expression operator

J

Jan Engelhardt

Hi,


I was told that order of evaluation is unspecified for functions, i.e.

int f = 0;
print_results(modify(&f), modify(&f), modify(&f));

where i.e. modify() increases f by one. In my case w/gcc, it was evaluated from
right-to-left (gcc does a nice stack optimization). Not what I expected though.
Anyway:

Taking a look at the C operator precedence table, I find

, sequential expression left-to-right

at the bottom. So what's wrong here?
 
I

Ian Woods

Hi,


I was told that order of evaluation is unspecified for functions, i.e.

int f = 0;
print_results(modify(&f), modify(&f), modify(&f));

where i.e. modify() increases f by one. In my case w/gcc, it was
evaluated from right-to-left (gcc does a nice stack optimization). Not
what I expected though. Anyway:

Taking a look at the C operator precedence table, I find

, sequential expression left-to-right

at the bottom. So what's wrong here?

From memory, the comma as an operator and a comma in a function invokation
are different things entirely. The order of evaluation of function
arguments is undefined.

Ian Woods
 
C

Christian Bau

Jan Engelhardt said:
Hi,


I was told that order of evaluation is unspecified for functions, i.e.

int f = 0;
print_results(modify(&f), modify(&f), modify(&f));

where i.e. modify() increases f by one. In my case w/gcc, it was evaluated
from
right-to-left (gcc does a nice stack optimization). Not what I expected
though.
Anyway:

Taking a look at the C operator precedence table, I find

, sequential expression left-to-right

at the bottom. So what's wrong here?


You are confusing "comma expression" and function arguments. If you look
at the syntax of a function call you will find that the arguments are
not "expression"s but "assignment-expression"s. For example, the
comma-exppression x,y,z is _not_ an assignment expression, so f(x,y,z)
has three arguments, and not one.
 
M

Mike Wahler

Jan Engelhardt said:
Hi,


I was told that order of evaluation is unspecified for functions,

For function arguments.
i.e.

int f = 0;
print_results(modify(&f), modify(&f), modify(&f));

where i.e. modify() increases f by one. In my case w/gcc, it was evaluated from
right-to-left (gcc does a nice stack optimization). Not what I expected
though.

Since the language does not specify order of
evaluation, any expectation about it is spurious.
Anyway:

Taking a look at the C operator precedence table, I find


, sequential expression left-to-right

Where did you encounter the term 'sequential expression'?
I don't find it in my copy of the C standard or in K&R.
Also, which operators are you referring to?
at the bottom. So what's wrong here?

What's wrong with what?

Order of evaluation and operator precedence are two
separate, distinct concepts. Given:


f(x) + g(y) * h(z);

Since order of evaluation is not specified,
we cannot know which of 'x', 'y' or 'z' is
evaluated first.

But precedence rules do specify that the
return values of 'g()' and 'h()' are multiplied
first (but we don't know which of 'g()' or 'h()'
is invoked first), then the return value of 'f()' is added
to that product.

Does that clarify things at all?

-Mike
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top