6
#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 Answers1

12

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621