Connecting Tech Pros Worldwide Help | Site Map

question about printf

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 02:45 PM
Ips
Guest
 
Posts: n/a
Default question about printf

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, 03:15 PM
madhur
Guest
 
Posts: n/a
Default 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, 03:15 PM
Karl Heinz Buchegger
Guest
 
Posts: n/a
Default 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, 03:15 PM
Rob Williscroft
Guest
 
Posts: n/a
Default 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, 03:16 PM
Sumit Rajan
Guest
 
Posts: n/a
Default 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, 03:17 PM
Old Wolf
Guest
 
Posts: n/a
Default 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).
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.