By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

how this is evaluated ?

P: n/a
sam
int i=0;

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

output:
4 3 2 1

why is not
1 2 3 4
Nov 13 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
sam wrote:
int i=0;

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


Does this help?
http://www.eskimo.com/~scs/C-faq/s3.html

Nov 13 '05 #2

P: n/a
sam wrote:
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

Nov 13 '05 #3

P: n/a


sam wrote:
int i=0;

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

output:
4 3 2 1

why is not
1 2 3 4


This is answered in the faq question 3.2 at
http://www.eskimo.com/~scs/C-faq/q3.2.html

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 13 '05 #4

P: n/a
Bruno Desthuilliers <bd***********@removeme.free.fr> spoke thus:
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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #5

P: n/a
In <bq**********@chessie.cirr.com> Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
Bruno Desthuilliers <bd***********@removeme.free.fr> spoke thus:
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.


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #6

P: n/a
sam wrote:
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.
--
Martin Ambuhl

Nov 13 '05 #7

P: n/a
Martin Ambuhl <ma*****@earthlink.net> spoke thus:
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?
;)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #8

P: n/a
Bruno Desthuilliers wrote:

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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Nov 13 '05 #9

P: n/a
Martin Ambuhl wrote:
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.

Nov 13 '05 #10

P: n/a
Bruno Desthuilliers <bd***********@removeme.free.fr> wrote in message news:<3f***********************@news.free.fr>...
sam wrote:
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.

--
Peter
Nov 13 '05 #11

P: n/a
The OP wrote:

int i=0;

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

output:
4 3 2 1

why is not
1 2 3 4
Martin Ambuhl wrote:
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.

"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in
news:3F**************@jpl.nasa.gov:
Please cite and quote the relevant FAQ.


http://www.eskimo.com/~scs/C-faq/q3.2.html

You think we make this stuff up E. Robert?

--
- Mark ->
--
Nov 13 '05 #12

P: n/a
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> writes:
Martin Ambuhl wrote:
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.


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)
Nov 13 '05 #13

P: n/a
"Mark A. Odell" wrote:
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote:
.... snip ...
Please cite and quote the relevant FAQ.


http://www.eskimo.com/~scs/C-faq/q3.2.html

You think we make this stuff up E. Robert?


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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #14

P: n/a
In <3F**************@jpl.nasa.gov> "E. Robert Tisdale" <E.**************@jpl.nasa.gov> writes:
Martin Ambuhl wrote:
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.


Why?
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #15

P: n/a
CBFalconer <cb********@yahoo.com> wrote in
news:3F***************@yahoo.com:
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote:

... snip ...
> Please cite and quote the relevant FAQ.


http://www.eskimo.com/~scs/C-faq/q3.2.html

You think we make this stuff up E. Robert?


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.

--
- Mark ->
--
Nov 13 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.