相关疑难解决方法(0)

如果我忘记关闭扫描设置会发生什么?

假设我忘了关闭]扫描仪的右方括号.那会发生什么?它会调用未定义的行为吗?

例:

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);
Run Code Online (Sandbox Code Playgroud)

我在编译时收到GCC的警告:

source_file.c: In function ‘main’:
source_file.c:11:5: warning: no closing ‘]’ for ‘%[’ format [-Wformat=]
     sscanf(str, "%s %[^h", s1, s2); /* UB? */
Run Code Online (Sandbox Code Playgroud)

和输出为

s1='Hello!' s2=''
Run Code Online (Sandbox Code Playgroud)

我也注意到了sscanf回报1.但到底是怎么回事?

我检查了C11标准,但没有找到与此相关的信息.

c scanf format-specifiers

11
推荐指数
1
解决办法
132
查看次数

标签 统计

c ×1

format-specifiers ×1

scanf ×1