简体   繁体   English

用scanf进行C输入

[英]C input with scanf

In C programming language the scanf() function reads from keyboard characters or numbers? 用C编程语言, scanf()函数从键盘字符或数字读取吗?

For example, if the format is %d and I write "1" or "a" , the scanf() reads only integer numbers and ignore other characters? 例如,如果格式为%d且我写"1""a" ,则scanf()仅读取整数,而忽略其他字符吗?

I have read on a book that the scanf() reads charcters from keyboard, and then converts the characters into data types specified by formats. 我读过一本书,其中scanf()从键盘读取字符,然后将字符转换为格式指定的数据类型。

Can anyone explain me. 谁能解释我。 Thanks in advance. 提前致谢。

%c is used to read characters %c用于读取字符

%d is for integers %d用于整数

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. 如果我被声明为整数,那么如果您尝试打印i的值,则它将打印整数。

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. 如果我被声明为字符,那么如果您尝试打印i的值,则它将打印字符。

Also check scanf:- 还要检查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. 并且,如果在成功读取任何数据之前发生任何一种情况,则返回EOF。

If an encoding error happens interpreting wide characters, the function sets errno to EILSEQ. 如果在解释宽字符时发生编码错误,则该函数将errno设置为EILSEQ。

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: 如果查看scanf()函数签名 ,它将返回一个int ,可用于检查扫描操作是否成功:

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. 并且,如果在成功读取任何数据之前发生任何一种情况,则返回EOF。

If an encoding error happens interpreting wide characters, the function sets errno to EILSEQ. 如果在解释宽字符时发生编码错误,则该函数将errno设置为EILSEQ。

scanf() reads charcters from stdin , and then converts the characters into data types specified by formats. scanf() reads charcters from stdin scanf() reads charcters from , and then converts the characters into data types specified by formats. ?

Man page on scanf clearly answers this, 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. 

Scanning stops when an input character does not match such a format character. 当输入字符与格式字符不匹配时,扫描将停止。 Scanning also stops when an input conversion cannot be made 当无法进行输入转换时,扫描也会停止

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

You are wondering infrastructure details of your communication with the program in a prompt(cmd, terminal etc.) environment i guess. 我想您想知道在提示(cmd,终端等)环境中与程序通信的基础结构详细信息。 When you write the code like: 当您编写如下代码时:

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

and compile and run, program stops and waits for the scanf line to be entered anything to the underlined environment such as cmd or terminal . 然后编译并运行,程序停止并等待scanf行输入任何带有下划线的环境(例如cmdterminal When you or your user start entering anything from the keyboard, program does nothing maybe buffering. 当您或您的用户开始从键盘输入任何内容时,程序可能不会执行任何缓冲操作。 But when you press enter, program tries to get the characters(up to the whitespace) and convert them to desired variable type that is integer, because you said so in the scanf function parameter. 但是,当您按Enter键时,程序会尝试获取字符(最多为空格)并将其转换为所需的整数变量类型,因为您在scanf函数参数中已这样说。 This mentioned conversion happens implicitly. 提到的转换隐式发生。 If you wrote: 如果您写了:

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

and enter some keys from the keyboard, then the underlined behavior of the conversion would be different. 并从键盘上输入一些键,则转换的下划线行为将有所不同。 For summary, logic is simple: if you define varible int , then the program tries to convert to int ; 概括地说,逻辑很简单:如果您定义variable int ,则程序将尝试转换为int if you define float , program tries to convert to float . 如果定义float ,则程序将尝试转换为float But all entered keys come from the character sequence and this is called string . 但是所有输入的键都来自字符序列,这称为字符串 I recommend you to read the usage of scanf and let it go. 我建议您阅读scanf的用法并放手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM