the order of printf parameter

Discussion in 'C Programming' started by marsarden, Jan 11, 2006.

  1. marsarden

    marsarden Guest

    --------pq.c--------
    #include <stdio.h>
    void main()
    {
    int a=2,*p,*q;
    p=&a;q=&a;
    printf("%d %d\n",*p++,*(q++));
    printf("%d\n",a);
    p=&a;q=&a;
    printf("%d %d\n",*p,(*q)++);
    }
    -----------------------------------------
    in gcc it output:

    2 2
    2
    3 2

    in vc6 it output:
    2 2
    2
    2 2

    anyone can tell me how the printf works?
    marsarden, Jan 11, 2006
    #1
    1. Advertising

  2. "marsarden" <> writes:
    > --------pq.c--------
    > #include <stdio.h>
    > void main()


    Find yourself a chalkboard and a piece of chalk (a whiteboard and a
    marker will do if necessary). Write 100 times:
    "main() returns int."

    Change the line to "int main(void)".

    > {
    > int a=2,*p,*q;
    > p=&a;q=&a;


    Ok, p and q both point to a, which has the value 2.

    > printf("%d %d\n",*p++,*(q++));


    This should always print "2 2". You increment both pointers after
    dereferencing them, which is fairly pointless, but it's harmless.

    > printf("%d\n",a);


    This will print "2".

    > p=&a;q=&a;


    Now p and q both point to a again.

    > printf("%d %d\n",*p,(*q)++);


    Here the third argument increments the object that q points to (namely a).

    But as for any function call, the order of evaluation of the arguments
    is unspecified.

    Finally, since main() returns int, you should have a "return 0;" here.

    > }
    > -----------------------------------------
    > in gcc it output:
    >
    > 2 2
    > 2
    > 3 2
    >
    > in vc6 it output:
    > 2 2
    > 2
    > 2 2


    Since the order of evaluation is unspecified, both results are valid.

    If you care about the output, write the call so that it will work
    properly and consistently regardless of the order of evaluation of the
    arguments. For example, do the increment before or after the call,
    not in the middle of it. If you keep your code simple and readable,
    you don't have to *care* about details like this.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jan 11, 2006
    #2
    1. Advertising

  3. marsarden

    Flash Gordon Guest

    Keith Thompson wrote:
    > "marsarden" <> writes:


    <snip>

    >> p=&a;q=&a;

    >
    > Now p and q both point to a again.
    >
    >> printf("%d %d\n",*p,(*q)++);

    >
    > Here the third argument increments the object that q points to (namely a).
    >
    > But as for any function call, the order of evaluation of the arguments
    > is unspecified.


    <snip>

    >> in gcc it output:
    >>
    >> 2 2
    >> 2
    >> 3 2
    >>
    >> in vc6 it output:
    >> 2 2
    >> 2
    >> 2 2

    >
    > Since the order of evaluation is unspecified, both results are valid.


    It's worse than that surely? The value of a is being modified by (*q)++
    and read for a purpose other than calculating the new value by *p with
    no intervening sequence point, so surely this is undefined behaviour?

    To the OP, undefined behaviour means that anything at all is valid, even
    the program causing the HD in the computer to spin up to 10000000rpm and
    then fly apart killing everyone in a 100 foot radius. Although in this
    case the results you are seeing are more likely.

    > If you care about the output, write the call so that it will work
    > properly and consistently regardless of the order of evaluation of the
    > arguments. For example, do the increment before or after the call,
    > not in the middle of it. If you keep your code simple and readable,
    > you don't have to *care* about details like this.


    Agreed.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, Jan 11, 2006
    #3
  4. marsarden wrote:
    > --------pq.c--------
    > #include <stdio.h>
    > void main()

    ^^^^
    Dead already.
    Martin Ambuhl, Jan 11, 2006
    #4
  5. marsarden

    Old Wolf Guest

    Keith Thompson wrote:
    > "marsarden" <> writes:
    >>
    >> int a=2,*p,*q;
    >> p=&a;q=&a;

    >
    > Ok, p and q both point to a, which has the value 2.
    >
    >> printf("%d %d\n",*p,(*q)++);

    >
    > Here the third argument increments the object that q points to (namely a).
    >
    > But as for any function call, the order of evaluation of the arguments
    > is unspecified.


    Unfortunately, *p and *q are the same object. So, as
    Flash (saviour of the universe) suggested, we definitiely
    have UB: there is no sequence point between the write of *q,
    and the read of *p.
    Old Wolf, Jan 12, 2006
    #5
  6. "Old Wolf" <> writes:
    > Keith Thompson wrote:
    >> "marsarden" <> writes:
    >>>
    >>> int a=2,*p,*q;
    >>> p=&a;q=&a;

    >>
    >> Ok, p and q both point to a, which has the value 2.
    >>
    >>> printf("%d %d\n",*p,(*q)++);

    >>
    >> Here the third argument increments the object that q points to (namely a).
    >>
    >> But as for any function call, the order of evaluation of the arguments
    >> is unspecified.

    >
    > Unfortunately, *p and *q are the same object. So, as
    > Flash (saviour of the universe) suggested, we definitiely
    > have UB: there is no sequence point between the write of *q,
    > and the read of *p.


    You're right, I missed that.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jan 12, 2006
    #6
  7. marsarden

    marsarden Guest

    thanks alot
    marsarden, Jan 16, 2006
    #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. ben
    Replies:
    4
    Views:
    614
    Martin Ambuhl
    Jun 26, 2004
  2. whatluo

    (void) printf vs printf

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

    order of printf

    happy, Jan 9, 2010, in forum: C Programming
    Replies:
    16
    Views:
    783
    Tim Rentsch
    Jan 13, 2010
  4. azza

    printf affects following printf/s

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

Share This Page