In article <47**********************@news.free.fr>,
Spoon <root@localhostwrote:
>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