问题
#include <stdio.h>
int main(void)
{
char b[5];
scanf("%4s%4s", b, b);
//My input: "qwer<Enter>sgsh<Enter>"
printf("%s", b);
//Output: sgsh
}
C99: Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression.
In this case, I am modifying the value of b
twice
. Isn't it undefined behavior
?
回答1:
From this scanf reference:
There is a sequence point after the action of each conversion specifier; this permits storing multiple fields in the same "sink" variable.
So what you're doing is defined and should work well.
来源:https://stackoverflow.com/questions/61650409/is-scanf4s4s-b-b-well-defined