简体   繁体   中英

C input with scanf

In C programming language the scanf() function reads from keyboard characters or numbers?

For example, if the format is %d and I write "1" or "a" , the scanf() reads only integer numbers and ignore other characters?

I have read on a book that the scanf() reads charcters from keyboard, and then converts the characters into data types specified by formats.

Can anyone explain me. Thanks in advance.

%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.

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.

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

Man page on scanf clearly answers this,

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

You are wondering infrastructure details of your communication with the program in a prompt(cmd, terminal etc.) environment i guess. 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 . 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. 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 ; if you define float , program tries to convert to 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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