0

在 C 编程语言中,scanf()函数从键盘字符或数字中读取?

例如,如果格式是%d并且我写"1"or "a",则scanf()只读整数并忽略其他字符?

我读过一本scanf()从键盘读取字符,然后将字符转换为格式指定的数据类型的书。

谁能给我解释一下。提前致谢。

4

5 回答 5

0

您想知道在提示(cmd、终端等)环境中与程序通信的基础架构细节,我猜。当您编写如下代码时:

int x;
scanf("%d", &x);

并编译和运行,程序停止并等待该scanf行被输入到带下划线的环境中,例如cmdor terminal。当您或您的用户开始从键盘输入任何内容时,程序可能不会做任何缓冲。但是当您按下回车键时,程序会尝试获取字符(直到空格)并将它们转换为所需的整数变量类型,因为您在scanf函数参数中是这样说的。上面提到的转换是隐式发生的。如果你写:

char str[20];
scanf("%s", str);

并从键盘输入一些键,那么带下划线的转换行为会有所不同。总而言之,逻辑很简单:如果定义变量int,则程序会尝试转换为int;如果您定义float,程序会尝试转换为float。但是所有输入的键都来自字符序列,这称为string。我建议您阅读scanf的用法 并放手。

于 2013-11-10T11:14:01.377 回答
0

%c is used to read characters

%d is for integers

When you write

scanf("%d",&i)

if i is declared as integer then if you try to print the value of i then it will print integer.

And when you write

scanf("%c",&i)

if i is declared as character then if you try to print the value of i then it will print charcter.

Also check scanf:-

Format specifiers: A sequence formed by an initial percentage sign (%) indicates a format specifier, which is used to specify the type and format of the data to be retrieved from the stream and stored into the locations pointed by the additional arguments.

Return Value

On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.

If a reading error happens or the end-of-file is reached while reading, the proper indicator is set (feof or ferror). And, if either happens before any data could be successfully read, EOF is returned.

If an encoding error happens interpreting wide characters, the function sets errno to EILSEQ.

于 2013-11-10T10:34:21.910 回答
0

If you look at the function signature of scanf(), it returns an int, which you can use to check whether the scan operation was successful or if it failed:

On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.

If a reading error happens or the end-of-file is reached while reading, the proper indicator is set (feof or ferror). And, if either happens before any data could be successfully read, EOF is returned.

If an encoding error happens interpreting wide characters, the function sets errno to EILSEQ.

于 2013-11-10T10:36:24.847 回答
0

scanf() reads charcters from标准输入, and then converts the characters into data types specified by formats.

scanf上的手册页清楚地回答了这个问题,

The scanf() family of functions scans input according to format as described 
below. This format may contain conversion specifications; the results from such
conversions, if any, are stored in the locations pointed to by the pointer 
arguments that follow format. Each pointer argument must be of a type that is 
appropriate for the value returned by the corresponding conversion specification. 
于 2013-11-10T10:39:25.130 回答
0

当输入字符与此类格式字符不匹配时,扫描停止。当无法进行输入转换时,扫描也会停止

http://www.linuxmanpages.com/man3/scanf.3.php

于 2013-11-10T10:49:04.107 回答