Connecting Tech Pros Worldwide Help | Site Map

question about printf

  #1  
Old July 22nd, 2005, 03:45 PM
Ips
Guest
 
Posts: n/a
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


  #2  
Old July 22nd, 2005, 04:15 PM
madhur
Guest
 
Posts: n/a

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.


  #3  
Old July 22nd, 2005, 04:15 PM
Karl Heinz Buchegger
Guest
 
Posts: n/a

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
  #4  
Old July 22nd, 2005, 04:15 PM
Rob Williscroft
Guest
 
Posts: n/a

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/
  #5  
Old July 22nd, 2005, 04:16 PM
Sumit Rajan
Guest
 
Posts: n/a

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.


  #6  
Old July 22nd, 2005, 04:17 PM
Old Wolf
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
the question about: printf("%d\t%d\t%d\t%d\t%d\t%d\n",i,++i,--i,i--,i++,-i--); WeiWangJi answers 6 March 9th, 2007 05:15 PM
A question about printf Why Tea answers 5 November 17th, 2006 11:35 AM
A small question about 'printf' volunteers@gmail.com answers 10 November 15th, 2005 03:57 AM
question about printf pratik answers 17 November 14th, 2005 07:31 AM