Connecting Tech Pros Worldwide Help | Site Map

question about printf

Ips
Guest
 
Posts: n/a
#1: Jul 22 '05
hello!

here's the code
int main()
{
int i=0;
printf("%d %d %d\n",i++,i++,i++);

return 0;
}

can anybody explain how tthe arguments are passed to this function? Why does
the output is : 3 2 1 and not 1 2 3 ?
Is this ANSI compliant or only gcc compiles it like this?

thanks
regaed,
Ips


madhur
Guest
 
Posts: n/a
#2: Jul 22 '05

re: question about printf



"Ips" <kornikx@poczta.onet.pl> wrote in message
news:cbbdan$drk$1@newshost.mot.com...[color=blue]
> hello!
>
> here's the code
> int main()
> {
> int i=0;
> printf("%d %d %d\n",i++,i++,i++);
>
> return 0;
> }
>
> can anybody explain how tthe arguments are passed to this function? Why[/color]
does[color=blue]
> the output is : 3 2 1 and not 1 2 3 ?
> Is this ANSI compliant or only gcc compiles it like this?
>
> thanks
> regaed,
> Ips
>
>[/color]

Hello
The arguments are evaluated from right to left. This is why the output is 3
2 1. Read about calling conventions.


Karl Heinz Buchegger
Guest
 
Posts: n/a
#3: Jul 22 '05

re: question about printf


Ips wrote:[color=blue]
>
> hello!
>
> here's the code
> int main()
> {
> int i=0;
> printf("%d %d %d\n",i++,i++,i++);
>
> return 0;
> }
>
> can anybody explain how tthe arguments are passed to this function? Why does
> the output is : 3 2 1 and not 1 2 3 ?
> Is this ANSI compliant or only gcc compiles it like this?[/color]

In short:
It is undefined which output is right.

In long:
You modify a value more then once between sequence points. The result
of doing that is undefined.
This question (or variations of that) come up very often. Please use
google to search the newsgroup archive for a more indepth discussion.

--
Karl Heinz Buchegger
kbuchegg@gascad.at
Rob Williscroft
Guest
 
Posts: n/a
#4: Jul 22 '05

re: question about printf


madhur wrote in news:2jsuqrF151fjsU1@uni-berlin.de in comp.lang.c++:
[color=blue]
> Hello
> The arguments are evaluated from right to left. This is why the output
> is 3 2 1. Read about calling conventions.
>[/color]

A common mistake, but nevertheless incorrect. Read about sequence Points.

To the OP, as Karl has already said the output is undefined. In
fact the whole programme exhibts Undefined Behaviour, the standard
nolonger specifies what, if anything, your programme does.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Sumit Rajan
Guest
 
Posts: n/a
#5: Jul 22 '05

re: question about printf



"Ips" <kornikx@poczta.onet.pl> wrote in message
news:cbbdan$drk$1@newshost.mot.com...[color=blue]
> hello!
>
> here's the code
> int main()
> {
> int i=0;
> printf("%d %d %d\n",i++,i++,i++);
>
> return 0;
> }
>
> can anybody explain how tthe arguments are passed to this function? Why[/color]
does[color=blue]
> the output is : 3 2 1 and not 1 2 3 ?
> Is this ANSI compliant or only gcc compiles it like this?
>
> thanks
> regaed,
> Ips[/color]


You may find the following links interesting:
http://www.eskimo.com/~scs/C-faq/s3.html
http://www.langer.camelot.de/Article...ncePoints.html

Regards,
Sumit.


Old Wolf
Guest
 
Posts: n/a
#6: Jul 22 '05

re: question about printf


Karl Heinz Buchegger <kbuchegg@gascad.at> wrote:[color=blue][color=green]
> > int main()
> > {
> > int i=0;
> > printf("%d %d %d\n",i++,i++,i++);
> >
> > return 0;
> > }
> >
> > can anybody explain how tthe arguments are passed to this function? Why does
> > the output is : 3 2 1 and not 1 2 3 ?
> > Is this ANSI compliant or only gcc compiles it like this?[/color]
>
> In short:
> It is undefined which output is right.[/color]

Both outputs are right. The program's output (and any other behavioural
aspects of it) is undefined. You might be thinking of
"implementation-defined" (in which case there would only be one correct
output).
Closed Thread