scanf()函数可以有一个scanset,它定义了scanf()要读取的一组字符。可以通过将字符放在方括号内来定义扫描集。
例如,以下scanset告诉scanf()只读取字符XYZ

%[XYZ]

使用扫描集时,scanf()会继续读取字符,直到遇到扫描集外的字符。

要指定反转集,请在集合前面追加^^告诉scanf()读取不在scanset中的字符。要指定扫描集中的范围,请使用连字符。 例如,这告诉scanf()接受字符AZ

%[A-Z]

扫描集区分大小写。 要扫描大写和小写字母,必须单独指定它们。要了解其工作原理,请尝试以下程序:

#include <stdio.h>

int main(void)
{
   int i;
   char str[80], str2[80];

   printf("testing the scanf scan set. %[abcdefg] \n");

   scanf("%d%[abcdefg]%s", &i, str, str2);
   printf("%d %s %s", i, str, str2);

   return 0;
}