Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > question about printf

Reply
Thread Tools

question about printf

 
 
Ips
Guest
Posts: n/a
 
      06-23-2004
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


 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      06-23-2004
Ips wrote:
>
> 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?


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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      06-23-2004
madhur wrote in news:(E-Mail Removed) in comp.lang.c++:

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


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/
 
Reply With Quote
 
Sumit Rajan
Guest
Posts: n/a
 
      06-23-2004

"Ips" <(E-Mail Removed)> wrote in message
news:cbbdan$drk$(E-Mail Removed)...
> 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



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.


 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      06-24-2004
Karl Heinz Buchegger <(E-Mail Removed)> wrote:
> > 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?

>
> In short:
> It is undefined which output is right.


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).
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
printf affects following printf/s azza C Programming 0 10-17-2010 09:43 AM
Extracting printf(...) from (void) printf(....) guru Perl Misc 8 02-03-2009 10:37 PM
(void) printf vs printf whatluo C Programming 29 09-08-2005 05:42 PM
bus error with printf line included, error without printf line? ben C Programming 4 06-26-2004 04:42 PM
Question about evaluating the arguments of printf (maybe OT?) Edith Gross C++ 2 11-02-2003 12:16 PM



Advertisments