question about printf

Discussion in 'C++' started by Ips, Jun 23, 2004.

  1. Ips

    Ips Guest

    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
    Ips, Jun 23, 2004
    #1
    1. Advertising

  2. 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
    Karl Heinz Buchegger, Jun 23, 2004
    #2
    1. Advertising

  3. madhur wrote in news: 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/
    Rob Williscroft, Jun 23, 2004
    #3
  4. Ips

    Sumit Rajan Guest

    "Ips" <> wrote in message
    news:cbbdan$drk$...
    > 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/Articles/VSJ/SequencePoints/SequencePoints.html

    Regards,
    Sumit.
    Sumit Rajan, Jun 23, 2004
    #4
  5. Ips

    Old Wolf Guest

    Karl Heinz Buchegger <> 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).
    Old Wolf, Jun 24, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edith Gross
    Replies:
    2
    Views:
    338
    =?iso-8859-1?Q?Juli=E1n?= Albo
    Nov 2, 2003
  2. ben
    Replies:
    4
    Views:
    614
    Martin Ambuhl
    Jun 26, 2004
  3. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,240
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    431
  5. guru
    Replies:
    8
    Views:
    281
Loading...

Share This Page