C: char 的 scanf 没有按预期工作
C: scanf for char not working as expected
我最近 运行在我的 PC 上安装了一个 c 程序。它有一个 for 循环,其中扫描了一些 char d。 for循环运行s 3次。在每个 运行ning 期间,它打印 运行ning 的计数,然后扫描 char d 的值。程序如下
#include<stdio.h>
int main(){
int f;
char d;
for(f=0;f<3;f++){
printf("Choice %d\n", f);
scanf("%c", &d);
}
return 0;
}
现在的问题是,当我运行程序时,当f为1时for跳过了scanf部分。
现在,如果我按如下方式更改代码
#include<stdio.h>
int main(){
int f;
int d;
for(f=0;f<3;f++){
printf("Choice %d\n", f);
scanf("%d", &d);
}
return 0;
}
现在程序运行良好。并且每次 for 循环迭代都会执行 scanf。
这似乎是什么问题?我的意思是当 d 是 int 类型时它工作正常,但是当 d 是 char 类型时它不能正常工作。
你必须改变
scanf("%c", &d);
至
scanf(" %c", &d);
^
|
否则,scanf()
会考虑之前输入的ENTER
按键。
注:
ENTER 按键生成一个 \n
,这是 %c
格式说明符的有效输入。在 %c
之前添加 space 告诉 scanf()
忽略所有前导的白色 space 类输入(包括之前存储的 \n
)并读取第一个非白色space 来自 stdin
的字符。
至于 %d
格式说明符的情况,它会在扫描数字输入之前消耗(并忽略)任何前导的白色 space 类输入,因此第二种情况没有遇到任何问题。
问题是当您输入 scanf("%c", &d);
的字符时,您按下了回车键。该字符由 scanf
消耗,换行符保留在标准输入流中 (stdin
)。下次调用 scanf
(with %c
) 时,它会在 stdin
中看到 \n
字符并使用它,因此不会等待进一步的输入。
要修复它,请更改
scanf("%c", &d);
至
scanf(" %c", &d);
// ^Note the space before %c
%c
之前的space指示scanf
扫描任意数量的白色space字符,包括none,遇到非字符时停止扫描-whitespace 字符。引用标准:
7.21.6.2 The fscanf function
[...]
- A directive composed of white-space character(s) is executed by reading input up to the
first non-white-space character (which remains unread), or until no more characters can
be read. The directive never fails.
使用 %d
的原因是 %d
格式说明符自动跳过白色 space 字符,因为 \n
是白色 space 字符, %d
不扫描它。再次引用标准:
7.21.6.2 The fscanf function
[...]
- Input white-space characters (as specified by the
isspace
function) are skipped, unless
the specification includes a [
, c
, or n
specifier. 284
我最近 运行在我的 PC 上安装了一个 c 程序。它有一个 for 循环,其中扫描了一些 char d。 for循环运行s 3次。在每个 运行ning 期间,它打印 运行ning 的计数,然后扫描 char d 的值。程序如下
#include<stdio.h>
int main(){
int f;
char d;
for(f=0;f<3;f++){
printf("Choice %d\n", f);
scanf("%c", &d);
}
return 0;
}
现在的问题是,当我运行程序时,当f为1时for跳过了scanf部分。 现在,如果我按如下方式更改代码
#include<stdio.h>
int main(){
int f;
int d;
for(f=0;f<3;f++){
printf("Choice %d\n", f);
scanf("%d", &d);
}
return 0;
}
现在程序运行良好。并且每次 for 循环迭代都会执行 scanf。
这似乎是什么问题?我的意思是当 d 是 int 类型时它工作正常,但是当 d 是 char 类型时它不能正常工作。
你必须改变
scanf("%c", &d);
至
scanf(" %c", &d);
^
|
否则,scanf()
会考虑之前输入的ENTER
按键。
注:
ENTER 按键生成一个
\n
,这是%c
格式说明符的有效输入。在%c
之前添加 space 告诉scanf()
忽略所有前导的白色 space 类输入(包括之前存储的\n
)并读取第一个非白色space 来自stdin
的字符。至于
%d
格式说明符的情况,它会在扫描数字输入之前消耗(并忽略)任何前导的白色 space 类输入,因此第二种情况没有遇到任何问题。
问题是当您输入 scanf("%c", &d);
的字符时,您按下了回车键。该字符由 scanf
消耗,换行符保留在标准输入流中 (stdin
)。下次调用 scanf
(with %c
) 时,它会在 stdin
中看到 \n
字符并使用它,因此不会等待进一步的输入。
要修复它,请更改
scanf("%c", &d);
至
scanf(" %c", &d);
// ^Note the space before %c
%c
之前的space指示scanf
扫描任意数量的白色space字符,包括none,遇到非字符时停止扫描-whitespace 字符。引用标准:
7.21.6.2 The fscanf function
[...]
- A directive composed of white-space character(s) is executed by reading input up to the first non-white-space character (which remains unread), or until no more characters can be read. The directive never fails.
使用 %d
的原因是 %d
格式说明符自动跳过白色 space 字符,因为 \n
是白色 space 字符, %d
不扫描它。再次引用标准:
7.21.6.2 The fscanf function
[...]
- Input white-space characters (as specified by the
isspace
function) are skipped, unless the specification includes a[
,c
, orn
specifier. 284