Suppose I forgot to close the right square bracket ]
of a scanset. What will happen then? Does it invoke Undefined Behavior?
Example:
char str[] = "Hello! One Two Three";
char s1[50] = {0}, s2[50] = {0};
sscanf(str, "%s %[^h", s1, s2); /* UB? */
printf("s1='%s' s2='%s'\n", s1, s2);
I get a warning from GCC when compiling:
source_file.c: In function ‘main’:
source_file.c:11:5: warning: no closing ‘]’ for ‘%[’ format [-Wformat=]
sscanf(str, "%s %[^h", s1, s2); /* UB? */
and the output as
s1='Hello!' s2=''
I've also noticed that the sscanf
returns 1. But what exactly is going on here?
I've checked the C11 standard, but found no information related to this.