Sequence point doubt

S

Spoon

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.
 
D

dj3vande

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
 
S

Spiros Bousbouras

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,260
Messages
2,571,039
Members
48,768
Latest member
first4landlord

Latest Threads

Top