C语言 scanf("%c", &c) 和 scanf("%c", &c) 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18491390/
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 07:18:26  来源:igfitidea点击:

Difference between scanf("%c", &c) and scanf(" %c", &c)

cscanf

提问by passmaster10

Consider the following C code snippet:

考虑以下 C 代码片段:

#include <stdio.h>

int main()
{
    int a;
    char c;
    scanf("%d",&a);
    scanf("%c",&c);
    printf("int=%d\n",a);
    printf("char=%c\n",c);
}

I'm able to input only the integer and not the character.The output is simply the integer value and no value is output for the second printf statement.

我只能输入整数而不是字符。输出只是整数值,第二个 printf 语句没有输出任何值。

However if I use a space before the format specifier:

但是,如果我在格式说明符之前使用空格:

scanf(" %c",&c);

It works as expected. Why is this the case?

它按预期工作。为什么会这样?

Someone told me it has something to do with clearing the input buffer. Could someone shed some light on the same?

有人告诉我这与清除输入缓冲区有关。有人可以解释一下吗?

回答by Jonathan Leffler

The difference between scanf("%c", &c1)and scanf(" %c", &c2)is that the format without the blank reads the next character, even if it is white space, whereas the one with the blank skips white space (including newlines) and reads the next character that is not white space.

scanf("%c", &c1)和之间的区别在于,scanf(" %c", &c2)没有空格的格式读取下一个字符,即使它是空格,而有空格的格式会跳过空格(包括换行符)并读取下一个不是空格的字符。

In a scanf()format, a blank, tab or newline means 'skip white space if there is any to skip'. It does not directly 'clear the input buffer', but it does eat any white space which looks similar to clearing the input buffer (but is quite distinct from that). If you're on Windows, using fflush(stdin)clears the input buffer (of white space and non-white space characters); on Unix and according to the C standard, fflush(stdin)is undefined behaviour.

在某种scanf()格式中,空白、制表符或换行符意味着“如果有任何要跳过的空白,则跳过”。它不直接“清除输入缓冲区”,但它会占用任何看起来类似于清除输入缓冲区的空白区域(但与之截然不同)。如果你在 Windows 上, usingfflush(stdin)清除输入缓冲区(空白和非空白字符);在 Unix 上,根据 C 标准,fflush(stdin)是未定义的行为。

Incidentally, if you typed the integer followed immediately by a carriage return, the output of your program ends with two newlines: the first was in cand the second in the format string. Thus, you might have seen:

顺便说一句,如果您输入整数后紧跟一个回车符,程序的输出将以两个换行符结束:第一个c在格式字符串中,第二个在格式字符串中。因此,您可能已经看到:

$ ./your_program
123
int=123
char=

$

That is, the scanf()reads the newline as its input. Consider an alternative input:

也就是说,scanf()读取换行符作为其输入。考虑一个替代输入:

$ ./your_program
123xyz
int=123
char=x
$

The integer input stopped when it read the 'x'; the character input therefore reads the 'x'.

整数输入在读取 'x' 时停止;因此字符输入读取'x'。

回答by Yu Hao

Because after you input the number and press ENTER, the new line stays in the buffer and will be processed by the second scanf.

因为在您输入数字并按 ENTER 后,新行将保留在缓冲区中并由第二个scanf.

In short, you saved new line in the variable c.

简而言之,您在变量中保存了新行c

However ,if you use

但是,如果您使用

scanf(" %c",&c);
//     ^

the space will consume the new line, which makes cthe value you expected.

空间将消耗新行,这会c产生您期望的值。

回答by phyrrus9

You have to pass a pointer to the data object specified by the format string, so

您必须传递一个指向格式字符串指定的数据对象的指针,因此

scanf("%c", c);

will actually pass the value of c, which in turn could cause a program fault,

实际上会传递 c 的值,这反过来可能会导致程序错误,

scanf("%c", &c);

will pass the address of c, allowing scanf to change the value of your copy.

将传递 c 的地址,允许 scanf 更改您的副本的值。

The space after the %c will force it to look for a character, AND THEN a space. If there is not a space, it will not read the character

%c 后面的空格将强制它查找一个字符,然后是一个空格。如果没有空格,则不会读取字符