the question about: printf("%d\t%d\t%d\t%d\t%d\t%d\n",i,++i,--i,i--,i++,-i--);

Discussion in 'C++' started by WeiWangJi, Mar 9, 2007.

  1. WeiWangJi

    WeiWangJi Guest

    int i=8;
    printf("%d\t%d\t%d\t%d\t%d\t%d\n",i,++i,--i,i--,i++,-i--);
    printf("%d\n", i);

    why the results is:

    8 8 7 8 8 -8
    7

    How to explain?
     
    WeiWangJi, Mar 9, 2007
    #1
    1. Advertising

  2. WeiWangJi wrote:
    > int i=8;
    > printf("%d\t%d\t%d\t%d\t%d\t%d\n",i,++i,--i,i--,i++,-i--);
    > printf("%d\n", i);
    >
    > why the results is:
    >
    > 8 8 7 8 8 -8
    > 7
    >
    > How to explain?
    >


    There is no explaination.

    Because you modify the same varaible more than once in the same function
    call your code is illegal, and it's behaviour is undefined.

    If you tried the same code on a differnt compiler you would probably get
    a different result.

    You could even see you program crash, because your code is illegal.

    john
     
    John Harrison, Mar 9, 2007
    #2
    1. Advertising

  3. WeiWangJi

    Vallabha Guest

    Re: the question about: printf("%d\t%d\t%d\t%d\t%d\t%d\n",i,++i,--i,i--,i++,-i--);

    On Mar 9, 11:20 am, "WeiWangJi" <> wrote:
    > int i=8;
    > printf("%d\t%d\t%d\t%d\t%d\t%d\n",i,++i,--i,i--,i++,-i--);
    > printf("%d\n", i);
    >
    > why the results is:
    >
    > 8 8 7 8 8 -8
    > 7
    >
    > How to explain?


    I guess you want to someone to explain the reason behind this output..
    if so, here is some help for you.

    cut down the sample to analyze the things:

    #include <stdio.h>

    int main()
    {
    int i =8;
    printf("%d\t %d\t %d\n", i,--i, i--);
    printf("%d \n", i);
    return 0;
    }

    Output is:
    % ./a.out
    7 7 8
    6

    printf calculates the arguments from right to left and prints them
    from left to right.. so i-- is computed first, then --i and then i.

    Cheers
    -Vallabha
    S7 Software Solutions
    http://www.s7solutions.com/
     
    Vallabha, Mar 9, 2007
    #3
  4. Re: the question about: printf("%d\t%d\t%d\t%d\t%d\t%d\n",i,++i,--i,i--,i++,-i--);

    Vallabha wrote:
    > On Mar 9, 11:20 am, "WeiWangJi" <> wrote:
    >
    >>int i=8;
    >>printf("%d\t%d\t%d\t%d\t%d\t%d\n",i,++i,--i,i--,i++,-i--);
    >>printf("%d\n", i);
    >>
    >>why the results is:
    >>
    >>8 8 7 8 8 -8
    >>7
    >>
    >>How to explain?

    >
    >
    > I guess you want to someone to explain the reason behind this output..
    > if so, here is some help for you.
    >
    > cut down the sample to analyze the things:
    >
    > #include <stdio.h>
    >
    > int main()
    > {
    > int i =8;
    > printf("%d\t %d\t %d\n", i,--i, i--);
    > printf("%d \n", i);
    > return 0;
    > }
    >
    > Output is:
    > % ./a.out
    > 7 7 8
    > 6
    >
    > printf calculates the arguments from right to left and prints them
    > from left to right.. so i-- is computed first, then --i and then i.
    >
    > Cheers
    > -Vallabha
    > S7 Software Solutions
    > http://www.s7solutions.com/
    >


    printf might be calculating things left to right on your compiler, but
    there is no requirement for it to do so

    I ran the OP code on my compiler and got this

    7 7 7 8 7 -8
    7

    I ran it with optmisation turned on and got this

    8 8 8 8 8 -8
    7

    I tried your code on my compiler and got this

    6 6 8
    6

    but with optmisation turned on this

    7 7 8
    6

    All this results are correct, none of the compilers have bugs, because
    quite simply the behaviour of the OP's program, and your program, is
    undefined by the C++ standard. One can of course provide an explaination
    why one particular compiler produces one particular output, but I don't
    think it's particularly useful to do so.

    john
     
    John Harrison, Mar 9, 2007
    #4
  5. Re: the question about: printf("%d\t%d\t%d\t%d\t%d\t%d\n",i,++i,--i,i--,i++,-i--);

    John Harrison a écrit :
    > Vallabha wrote:
    >> On Mar 9, 11:20 am, "WeiWangJi" <> wrote:
    >>
    >>> int i=8;
    >>> printf("%d\t%d\t%d\t%d\t%d\t%d\n",i,++i,--i,i--,i++,-i--);
    >>> printf("%d\n", i);
    >>>
    >>> why the results is:
    >>>
    >>> 8 8 7 8 8 -8
    >>> 7
    >>>
    >>> How to explain?

    >>
    >>
    >> I guess you want to someone to explain the reason behind this output..
    >> if so, here is some help for you.
    >>
    >> cut down the sample to analyze the things:
    >> [snip]
    >>
    >> printf calculates the arguments from right to left and prints them
    >> from left to right.. so i-- is computed first, then --i and then i.
    >>


    >>

    >
    > printf might be calculating things left to right on your compiler, but
    > there is no requirement for it to do so
    > [snip]
    >
    > All this results are correct, none of the compilers have bugs, because
    > quite simply the behaviour of the OP's program, and your program, is
    > undefined by the C++ standard. One can of course provide an explaination
    > why one particular compiler produces one particular output, but I don't
    > think it's particularly useful to do so.


    No since it is certainly performance related and dependant on strategies
    but also compiler flags.

    OP: Google for "sequence point c++"
    It will explain what is a sequence point and may tell why modifying a
    value multiple time in a sequence point is UB.

    Michael
     
    Michael DOUBEZ, Mar 9, 2007
    #5
  6. WeiWangJi

    Sarath Guest

    Sarath, Mar 9, 2007
    #6
  7. WeiWangJi

    Clark Cox Guest

    Re: the question about: printf("%d\t%d\t%d\t%d\t%d\t%d\n",i,++i,--i,i--,i++,-i--);

    On 2007-03-08 23:02:19 -0800, "Vallabha" <> said:

    > On Mar 9, 11:20 am, "WeiWangJi" <> wrote:
    >> int i=8;
    >> printf("%d\t%d\t%d\t%d\t%d\t%d\n",i,++i,--i,i--,i++,-i--);
    >> printf("%d\n", i);
    >>
    >> why the results is:
    >>
    >> 8 8 7 8 8 -8
    >> 7
    >>
    >> How to explain?

    >
    > I guess you want to someone to explain the reason behind this output..
    > if so, here is some help for you.


    You aren't helping; you're confusing the issue. The result of that code
    is completely undefined.

    > cut down the sample to analyze the things:
    >
    > #include <stdio.h>
    >
    > int main()
    > {
    > int i =8;
    > printf("%d\t %d\t %d\n", i,--i, i--);
    > printf("%d \n", i);
    > return 0;
    > }
    >
    > Output is:
    > % ./a.out
    > 7 7 8
    > 6


    .... or anything else.

    > printf calculates the arguments from right to left and prints them


    This is simply not true; there is no defined order in which arguments
    to a function are evaluated.

    > from left to right.. so i-- is computed first, then --i and then i.



    --
    Clark S. Cox III
     
    Clark Cox, Mar 9, 2007
    #7
    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:
    346
    =?iso-8859-1?Q?Juli=E1n?= Albo
    Nov 2, 2003
  2. ben
    Replies:
    4
    Views:
    634
    Martin Ambuhl
    Jun 26, 2004
  3. whatluo

    (void) printf vs printf

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

    printf affects following printf/s

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

Share This Page