Function prototype:
int scanf(constchar*format,...);This function is defined in the header file stdio. H. Therefore, you must add # include <stdio. h> when using the scanf function. It is a format input function, that is, data is input from the keyboard to the specified variable in the format specified by the user. The scanf () function is a common subprogram that reads content from the standard input stream stdio (standard input device, usually a keyboard). It can read multiple characters in a descriptive format, and save it in the corresponding address variable. The call format is scanf ("<format description string>", <variable address>). The variable address must be valid and consistent with the format description.
Return Value:If the scanf () function returns a successful result, the number of data values assigned is returned. If the value is read to the end of the file or an error occurs, EOF is returned and the returned value is int type. For example:
scanf
(
"%d%d"
,&a,&b);If both A and B are read successfully, the return value of scanf is 2. If only a is read successfully, the return value is 1. If both A and B are not read successfully, the return value is 0.
The following introduces the issues that need to be discussed. We often encounter this when using the getchar function:// ---------------------------------------------------------- Int ch;
Ch = getchar ();//--------------------------------------------------------
Why is Ch declared as an integer and we actually need it to read characters?
The answer is, do you still remember the returned EDF value when a getchar error occurs? Because EOF is an integer value, it has more digits than the character type. When ch is declared as an integer, it can be interpreted as EOF when a character read from the input device fails. This also means that the ch of the accepted character must be large enough to accommodate EOF, Which is why ch uses an integer value. The character is only a "small integer", so using an integer variable to hold the character value does not cause any problems.
Discussing the getchar () function