C语言 简单的 C scanf 不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3744776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:27:50  来源:igfitidea点击:

Simple C scanf does not work?

cconsolescanf

提问by John

If I try something such as:

如果我尝试诸如:

int anint;
char achar;

printf("\nEnter any integer:");
scanf("%d", &anint);
printf("\nEnter any character:");
scanf("%c", &achar);
printf("\nHello\n");
printf("\nThe integer entered is %d\n", anint);
printf("\nThe char entered is %c\n", achar);

It allows entering an integer, then skips the second scanfcompletely, this is really strange, as when I swap the two (the charscanf first), it works fine. What on earth could be wrong?

它允许输入一个整数,然后scanf完全跳过第二个,这真的很奇怪,因为当我交换两个(char先是 scanf)时,它工作正常。到底有什么问题?

回答by codaddict

When reading input using scanf, the input is read after the return key is pressed but the newline generated by the return key is not consumed by scanf, which means the next time you read a charfrom standard input there will be a newline ready to be read.

使用 读取输入时scanf,按下回车键后读取输入,但回车键生成的换行符不会被 消耗scanf,这意味着下次您char从标准输入读取 a 时,将有一个换行符可供读取。

One way to avoid is to use fgetsto read the input as a string and then extract what you want using sscanfas:

避免的一种方法是使用fgets将输入作为字符串读取,然后使用以下方式提取您想要的内容sscanf

char line[MAX];

printf("\nEnter any integer:");
if( fgets(line,MAX,stdin) && sscanf(line,"%d", &anint)!=1 ) 
   anint=0;

printf("\nEnter any character:");
if( fgets(line,MAX,stdin) && sscanf(line,"%c", &achar)!=1 ) 
   achar=0;

Another way to consume the newline would be to scanf("%c%*c",&anint);. The %*cwill read the newline from the buffer and discard it.

使用换行符的另一种方法是scanf("%c%*c",&anint);. 该%*c会从缓冲器读取换行符并将其丢弃。

You might want to read this:

您可能想阅读以下内容:

C FAQ : Why does everyone say not to use scanf?

C FAQ : 为什么大家都说不要用scanf?

回答by caf

The other answers are correct - %cdoes not skip whitespace. The easiest way to make it do so is to place whitespace before the %c:

其他答案是正确的 -%c不会跳过空格。最简单的方法是在 之前放置空格%c

scanf(" %c", &achar);

(Any whitespace in the format string will make scanfconsume all consecutive whitespace).

(格式字符串中的任何空格都会scanf消耗所有连续的空格)。

回答by Jonathan Leffler

It doesn't skip the second scanf(); the second scanf()reads the newline left behind by the first scanf(). Most format codes skip white space; the %cformat does not skip white space.

它不会跳过第二个scanf();第二个scanf()读取第一个留下的换行符scanf()。大多数格式代码跳过空格;该%c格式不跳过空白。

回答by Dale Diaz

calling getchar()before scanfwill also purge the stored line break. More lightweight but more situational

调用getchar()beforescanf也将清除存储的换行符。更轻量级但更具情境性

char input_1;
char input_2;
getchar();
scanf("%c", &input_1);
getchar();
scanf("%c", &input_2);

will flush the line breaks, more useful in consecutive lines of code where you know it's only one queued value and not a string

将刷新换行符,在您知道它只是一个排队值而不是字符串的连续代码行中更有用

回答by rkellerm

Try also _flushall() after each printf call. . Basically, by default MS's C++ buffers stream output, and the the flushing causes the output stream to empty.

在每次 printf 调用后也尝试 _flushall()。. 基本上,默认情况下 MS 的 C++ 缓冲流输出,刷新导致输出流为空。