how this is evaluated ?

B

Bruno Desthuilliers

sam said:
int i=0;

printf("%d %d %d %d",++i,++i,++i,++i);

output:
4 3 2 1

why is not
1 2 3 4

[laotseu@localhost dev]$ gcc -Wall -ansi -pedantic -oorder order.c
order.c: In function `main':
order.c:6: warning: operation on `i' may be undefined
order.c:6: warning: operation on `i' may be undefined
order.c:6: warning: operation on `i' may be undefined

AFAIK, the evaluation order of a function's args is
implementation-dependant (at least this is what I read in my K&R2).

HTH
Bruno
 
C

Christopher Benson-Manica

Bruno Desthuilliers said:
AFAIK, the evaluation order of a function's args is
implementation-dependant (at least this is what I read in my K&R2).

Or entirely undefined, as in this case.
 
D

Dan Pop

In said:
Or entirely undefined, as in this case.

It is actually unspecified. The undefined behaviour in this example is
not a consequence of the evaluation order being unspecified, it's a
consequence of the lack of sequence points between the evaluation of the
arguments (the standard allows parallel evaluation).

Dan
 
M

Martin Ambuhl

sam said:
int i=0;

printf("%d %d %d %d",++i,++i,++i,++i);

output:
4 3 2 1

why is not
1 2 3 4

Because it's not
42 42 42 42

Please check the FAQ before posting. Then you would know that your
question is old, tired, and more than adequately answered already -- in
countless threads started by other people as clueless as you, as well as in
the the FAQ.
 
C

Christopher Benson-Manica

Martin Ambuhl said:
Please check the FAQ before posting. Then you would know that your
question is old, tired, and more than adequately answered already -- in
countless threads started by other people as clueless as you, as well as in
the the FAQ.
^^^^^^^

Was was that intentional, or was it just a quirk quirk of the mind?
;)
 
E

E. Robert Tisdale

Martin said:
Please check the FAQ before posting.
Then you would know that your question is old, tired
and more than adequately answered already --
in countless threads started by other people as clueless as you,
as well as in the the FAQ.

Please cite and quote the relevant FAQ.
 
K

Kevin Goodsell

Bruno said:
AFAIK, the evaluation order of a function's args is
implementation-dependant (at least this is what I read in my K&R2).

Strictly speaking, order of argument evaluation is unspecified. This is
different from undefined and implementation-defined. Unspecified means
there is some set of options the implementation must choose from.
Undefined means the standard has nothing to say about it (so the
implementation can choose from potentially infinite options).
Implementation-defined means that the implementation must choose and
document the method it uses. As far as I know, argument evaluation order
need not be documented by the implementation, so while you may be able
to determine it experimentally, you can never be completely sure that it
doesn't use a different order in some circumstances.

-Kevin
 
P

Peter Nilsson

Bruno Desthuilliers said:
sam said:
int i=0;

printf("%d %d %d %d",++i,++i,++i,++i);

output:
4 3 2 1

why is not
1 2 3 4

[laotseu@localhost dev]$ gcc -Wall -ansi -pedantic -oorder order.c
order.c: In function `main':
order.c:6: warning: operation on `i' may be undefined
order.c:6: warning: operation on `i' may be undefined
order.c:6: warning: operation on `i' may be undefined

This is in the FAQ.
AFAIK, the evaluation order of a function's args is
implementation-dependant (at least this is what I read in my K&R2).

No, it's unspecified. Even implementations which require arguments to
be placed on a hardware stack in a specific order need not evaluate
argument expressions in that (or its reverse) order.
 
K

Keith Thompson

E. Robert Tisdale said:
Please cite and quote the relevant FAQ.

I don't think the FAQ directly addresses order of evaluation of
function arguments or the lack of sequence points between them, but
question 3.2 comes close.
 
M

Mark A. Odell

I suggest you are better off ignoring Trollsdale unless he gives
his usual erroneous advice. The above is a pure troll, and won't
lead anyone astray.

Good point. Boy, I hope he really doesn't work at jpl.nasa.gov, we don't
need anymore Mars space craft losses.
 

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
474,310
Messages
2,571,604
Members
48,416
Latest member
LelandBrie

Latest Threads

Top