Sequence point doubt

Discussion in 'C Programming' started by Spoon, Nov 19, 2007.

  1. Spoon

    Spoon Guest

    Hello,

    Is the following code valid:

    char buf[1024];
    int size = 666;
    size += sprintf(buf+size, "%d", size);
    size += sprintf(buf+size, "%d", size);

    (Expected behavior: at offset 666, buf contains "666669\0".)

    Is it equivalent to the following code:

    char buf[1024];
    int size = 666;
    int temp = sprintf(buf+size, "%d", size);
    size += temp;
    int temp = sprintf(buf+size, "%d", size);
    size += temp;

    AFAIU, the function call is a sequence point, therefore size can only be
    updated after sprintf has been evaluated with the "old" value of size.

    This reasoning might be incorrect if sprintf were a macro, right?
    (Can it be?)

    Regards.
    Spoon, Nov 19, 2007
    #1
    1. Advertising

  2. Spoon

    Guest

    In article <47419cea$0$4790$>,
    Spoon <root@localhost> wrote:
    >Hello,
    >
    >Is the following code valid:
    >
    >char buf[1024];
    >int size = 666;
    >size += sprintf(buf+size, "%d", size);
    >size += sprintf(buf+size, "%d", size);
    >
    >(Expected behavior: at offset 666, buf contains "666669\0".)
    >
    >Is it equivalent to the following code:
    >
    >char buf[1024];
    >int size = 666;
    >int temp = sprintf(buf+size, "%d", size);
    >size += temp;
    >int temp = sprintf(buf+size, "%d", size);
    >size += temp;
    >
    >AFAIU, the function call is a sequence point, therefore size can only be
    >updated after sprintf has been evaluated with the "old" value of size.


    That's correct, though the reason I'd give is that both of the places
    where size is evaluated in the sprintf call are used to determine the
    value that is eventually stored into size (they're needed for the call
    to sprintf, and the return from sprintf is added to the old value of
    size).


    >This reasoning might be incorrect if sprintf were a macro, right?
    >(Can it be?)


    Any standard library function is allowed to be a macro, but in most of
    them are required to act enough like functions that you don't usually
    need to care about the difference.

    The only thing I can think of is that the return value doesn't depend
    on the buffer you ask sprintf to print into, but I can't come up with a
    reasonable macro implementation that would break this. So I'll go
    ahead and claim that this is well-defined in every possible case,
    secure in the knowledge that the language lawyers who have finished
    their morning coffee will give several examples that will prove me
    wrong.


    dave
    , Nov 19, 2007
    #2
    1. Advertising

  3. On 19 Nov, 14:25, Spoon <root@localhost> wrote:
    > Hello,
    >
    > Is the following code valid:
    >
    > char buf[1024];
    > int size = 666;
    > size += sprintf(buf+size, "%d", size);
    > size += sprintf(buf+size, "%d", size);
    >
    > (Expected behavior: at offset 666, buf contains "666669\0".)


    Yes , it's valid. I would expect the same behaviour.

    > Is it equivalent to the following code:
    >
    > char buf[1024];
    > int size = 666;
    > int temp = sprintf(buf+size, "%d", size);
    > size += temp;
    > int temp = sprintf(buf+size, "%d", size);


    You are redefining temp.

    > size += temp;


    Apart from the mistake I pointed out they are equivalent.

    > AFAIU, the function call is a sequence point, therefore size can only be
    > updated after sprintf has been evaluated with the "old" value of size.


    You have a sequence point just after the arguments to sprintf()
    have been evaluated and before the actual call and another
    sequence point just before sprintf() returns.
    Spiros Bousbouras, Nov 19, 2007
    #3
    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. Bob Nelson

    doubt about doubt

    Bob Nelson, Jul 28, 2006, in forum: C Programming
    Replies:
    11
    Views:
    593
  2. Saraswati lakki
    Replies:
    0
    Views:
    1,283
    Saraswati lakki
    Jan 6, 2012
  3. Replies:
    0
    Views:
    536
  4. Peter Otten
    Replies:
    2
    Views:
    102
    Cousin Stanley
    Aug 10, 2013
  5. Terry Reedy
    Replies:
    0
    Views:
    105
    Terry Reedy
    Aug 10, 2013
Loading...

Share This Page