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

operator ++

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

I tried the code both in lcc32 and gcc both are giving same output

but for below code
printf("%d %d",p(),k());

function p() is called first in both lcc and gcc.
Nov 13 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

"sam" <sh**********@hotmail.com> wrote in message
news:25**************************@posting.google.c om...
int i=0;

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

output:
4 3 2 1

why is not
1 2 3 4

I tried the code both in lcc32 and gcc both are giving same output

but for below code
printf("%d %d",p(),k());

function p() is called first in both lcc and gcc.


because having more than one ++ operator on a varibale within the same
statement is undefined baheaviour.

int i=0;
printf("%d %d %d %d",++i,++i,++i,++i);

could print out
1) 1 1 1 1
2) 4 4 4 4
3) A N U S

or infact anything as it is undefined behaviour

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

I tried the code both in lcc32 and gcc both are giving same output

but for below code
printf("%d %d",p(),k());

function p() is called first in both lcc and gcc.


In ISO-C 99, section 6.5.2, item 10:

The order of evaluation of the function designator, the actual
arguments, and subexpressions within the actual arguments is
unspecified, but there is a sequence point before the actual call.

Similarly to your post on "evaluation order" answered by others, the
evaluation order is undefined. Thus, you cannot meaningfully update a
global variable (either directly or indirectly via a function) and rely
on any evaluation order to obtain a correct result. The meaning of
undefined is explained in http://www.eskimo.com/~scs/C-faq/q11.33.html

Nov 13 '05 #3

P: n/a
sh**********@hotmail.com (sam) wrote:
int i=0;

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


This is a newsgroup, not a chat room. You asked the very same question
two hours ago and got a perfectly good answer: RTFFAQ!

Richard
Nov 13 '05 #4

P: n/a
too much caffeine?
Calm down
Nov 13 '05 #5

P: n/a
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message news:<bq**********@news.freedom2surf.net>...
"sam" <sh**********@hotmail.com> wrote in message
news:25**************************@posting.google.c om...
int i=0;

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

output:
4 3 2 1

why is not
1 2 3 4

I tried the code both in lcc32 and gcc both are giving same output

but for below code
printf("%d %d",p(),k());

function p() is called first in both lcc and gcc.
because having more than one ++ operator on a varibale within the same
statement is undefined baheaviour.

You got the answer right but the reasoning wrong. Not in the *same
statment* but between two sequence points .. for instance in :
if(a&&b||c) ...
there is a sequence point at both the && and the || at which the side
effects of the previous part of the execution are required to be completed.
(Still this is one single statement).

int i=0;
printf("%d %d %d %d",++i,++i,++i,++i);

could print out
1) 1 1 1 1
2) 4 4 4 4
3) A N U S What intelligent undefined behaviour !!!!! ;)
or infact anything as it is undefined behaviour

Nov 13 '05 #6

P: n/a
Groovy hepcat sam was jivin' on 2 Dec 2003 05:12:43 -0800 in
comp.lang.c.
operator ++'s a cool scene! Dig it!
int i=0;

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

output:
4 3 2 1

why is not
1 2 3 4

I tried the code both in lcc32 and gcc both are giving same output

but for below code
printf("%d %d",p(),k());

function p() is called first in both lcc and gcc.


Once again, as you have been told before, order of function argument
evaluation is unspecified and modifying an object more than once
without an intervening sequence point causes undefined behaviour.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 13 '05 #7

P: n/a
Groovy hepcat kikko was jivin' on Tue, 02 Dec 2003 15:53:32 GMT in
comp.lang.c.
Re: operator ++'s a cool scene! Dig it!
too much caffeine?
Calm down


What the heck are you talking about? When following up a newsgroup
post, quote (the relevant portions of) the post to which you are
replying (as I have quoted you above). Provide some context, otherwise
noone has any idea what you're on about.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 13 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.