扫描集的解释
When it is given helloWORLD
, the conversion specification %[A-Z]
fails immediately because the h
is not an upper-case letter. Therefore, scanf()
returns 0, indicating that it did not successfully convert anything. If you tested the return value, you'd know that.
When it is given HELLoworlD
, the scanset matches the HELL
and stops at the first o
. The format string also attempts to match a literal s
, but there's no way for scanf()
to report that it fails to match that after matching HELL
.
Buffer overflow
Note that %[A-Z]
is in general dangerous (as is %s
) because there is no constraint on the number of characters read. If you have:
char str[50];
then you should use:
if (scanf("%49[A-Z]", str) != 1)
...some problem in the scan...
Also note that there is a 'difference by one' between the declared length of str
and the number in the format string. This is awkward; there's no way to provide that number as an argument to scanf()
separate from the format string (unlike printf()
), so you may end up creating the format string on the fly:
int scan_upper(char *buffer, size_t buflen)
{
char format[16];
if (buflen < 2)
return EOF; // Or other error indication
snprintf(format, sizeof(format), "%%%zu[A-Z]", buflen-1); // Check this too!?
return scanf(format, buffer);
}