operator ++

S

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.
 
A

Allan Bruce

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

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
 
J

Jonas Mellin

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

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
 
R

Richard Bos

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
 
A

Anupam

Allan Bruce said:
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 !!!!! ;)
 
P

Peter Shaggy Haywood

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"?
 
P

Peter Shaggy Haywood

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"?
 

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

No members online now.

Forum statistics

Threads
474,109
Messages
2,570,671
Members
47,263
Latest member
SyreetaGru

Latest Threads

Top