Function Name: scanf
Function: format the input.
Usage: int scanf (char * Format [, argument,...]);
The scanf () function is a common terminal formatting input function that reads input information from the standard input device (keyboard. You can read any inherent type of data and automatically convert the value into an appropriate internal format.
The call format is scanf ("<formatted string>", <Address Table> );
The scanf () function returns the number of successfully assigned data items. If an error occurs, EOF is returned.
Its Control string consists of three types of characters:
1. Format the specifier;
2. Blank space character;
3. Non-blank characters;
The format of the formatted string is generally [value assignment suppression Mark] [field width] [precision] type description, where the items in square brackets [] are optional.
Value assignment suppression mark *: scanf converts the character according to the current conversion specifier, but ignores the converted result, that is, it is not assigned to any variable.
Field width: used to specify the width of the input field. If it is not specified, the end tag is blank.
Precision: it is a character, which can be h, L, or l. It is used to change the meaning of the type specifiers after it.
L/L length modifier input "long" Data
H-length modifier input "short" Data
HH, ll, same as H, l, but only valid for c99.
H: used before the type specifiers D, I, O, U, X, X, and N. The specified parameter is a pointer to the short Int or unsigned short Int.
L: used before the type specifiers D, I, O, U, X, X, and N. The specified parameter is a pointer to long or unsigned long. Before the type specifiers A, A, E, E, F, F, G, and G, the specified parameter is a pointer to a double.
L: Before the type specifiers A, A, E, E, F, F, G, and G, the specified parameter is a pointer to long double.
Hh: used before the type specifiers D, I, O, U, X, X, and N. The specified parameter is a pointer to signed Char or unsigned char.
LL: Before the type specifiers D, I, O, U, X, X, and N, the specified parameter is a pointer to long or unsigned long.
Conversion specifier
Format Character Description
% A reads a floating point value (valid only for c99)
% A same as above
% C reads one character
% D read a decimal integer
% I reads decimal, octal, and hexadecimal Integers
% O read an octal integer
% X reads hexadecimal Integers
% X same as above
% S reads a string and adds an empty character at the end.
% F reads a floating point number
% F same as above
% E Same as above
% E Same as above
% G same as above
% G same as above
% P reads a pointer
% U reads an unsigned decimal integer
% N number of equivalent characters that have been read to this point
% [...] Scan Character Set combination, read only the characters listed in square brackets. If a character does not match, reaches the specified width, or you press enter, stop reading immediately. To read] characters, you should first list it []...]. Add a null character to the end of the string.
% [^...] Is the same as [...], but only characters not listed in square brackets are read.
% Read %. No value assignment is performed.
White space characters
The blank character will slightly remove one or more blank characters in the reading operation of the scanf () function. The blank character can be space, tab, newline, and so on until the first non-blank character appears.
Non-blank characters
A non-blank character will cause the scanf () function to remove the same character as this non-blank character during reading.
Scanf ()Use of function control strings
Example 1.
# Include "stdio. H"
Int main (void)
{
Int A, B, C;
Scanf ("% d", & A, & B, & C );
Printf ("% d, % d, % d/N", A, B, C );
Return 0;
}
Enter three values as follows:
3 □4 □5 (enter the values of A, B, and C)
3, 4, 5 (values of A, B, and C output by printf)
(1) The & operator in & A, & B, and & C obtains the memory addresses of these three variables respectively.
(2) "% d" is to input three values in the decimal value format. You can use one or more spaces, Tab keys, and enter keys to separate data.
Example 2.
# Include "stdio. H"
Int main (void)
{
Int A, B, C;
Scanf ("% d, % d, % d", & A, & B, & C );
Printf ("% d, % d, % d/N", A, B, C );
Return 0;
}
Enter three values as follows:
3, 4, 5 (enter the values of A, B, and C)
Or
3, □4, □5 (enter the values of A, B, and C)
3, □□□4, □5 (enter the values of A, B, and C)
......
They are all legal, but "," must be followed by a number, for example:
3 □, 4, □5 is invalid and the program fails.
Another example is:
1. the variables in sacnf () must use addresses.
Int A, B;
Scanf ("% d", a, B); // Error
Scanf ("% d", & A, & B );
2. the scanf () format control string can use other non-blank characters, but these characters must be entered during input.
Example:
Scanf ("% d, % d", & A, & B );
Input: 3, 4 (the comma corresponds to the comma in "% d, % d)
Scanf ("A = % d, B = % d", & A, & B );
Input: A = 3, B = 4 ("A =", "B =", comma and "A =" in "% d, % d ", "B =" corresponds to a comma)
3. When "% C" is used for input, spaces and escape characters are both valid characters.
Example:
Scanf ("% C", & C1, & C2, & C3 );
Input: A □b □c
Result: A → C1, □→ C2, B → C3 (other discarded)
When the scanf () function receives input data, it ends a data input in the following circumstances: (instead of ending the scanf function, the scanf function only has data in each data field and ends after pressing enter ).
① In case of space, "enter", and "Skip" keys.
② End with width.
③ Illegal input.
Scanf ()Common Errors and Countermeasures for using function control strings
Question 1: Does the scanf () function correctly accept strings with spaces? For example, I love you!
# Include <stdio. h>
Int main (void)
{
Char STR [80];
Scanf ("% s", STR );
Printf ("% s", STR );
Return 0;
}
Input: I live you!
Output: I
When the scanf () function receives input data, it ends a data input in the following circumstances: (instead of ending the scanf function, the scanf function only has data in each data field and ends after pressing enter ).
① In case of space, "enter", and "Skip" keys.
② End with width.
③ Illegal input.
Therefore, the above program cannot achieve the expected purpose. scanf () scans the space after "I" and considers the assignment of STR to an end, and ignores the "love you! ". Note that" love you! "It's still in the keyboard buffer zone (I have seen this on the internet, but after debugging, we will find that, in fact, the first and last pointers of the buffer string are equal, that is, the buffer is cleared, the scanf () function should only scan the stdin stream, and the residual information is in stdin ). Let's change the above program to verify it:
# Include <stdio. h>
Int main (void)
{
Char STR [80];
Char str1 [80];
Char str2 [80];
Scanf ("% s", STR);/* enter: I love you! */
Printf ("% s", STR );
Sleep (5);/* Wait 5 seconds to tell you where the program is running */
Scanf ("% s", str1);/* you do not need to enter the two statements, but scan the keyboard disk buffer */
Scanf ("% s", str2);/* you do not need to enter the two statements, but scan the keyboard disk buffer */
Printf ("/n % s", str1 );
Printf ("/n % s", str2 );
Return 0;
}
Input: I love you!
Output:
I
Love
You!
Okay, the reason is clear. Can the scanf () function complete this task? The answer is yes! Do not forget that the scanf () function has a % [] format controller. Please refer to the following program:
# Include <stdio. h>
Int main (void)
{
Char string [50];
/* Scanf ("% s", string); cannot receive space characters */
Scanf ("% [^/n] s", string );
Printf ("% s/n", string );
Return 0;
}
Question 2: residual information of the Keyboard Buffer
# Include <stdio. h>
Int main (void)
{
Int;
Char C;
Do
{
Scanf ("% d", & );
Scanf ("% C", & C );
Printf ("A = % d c = % C/N", a, c );
/* Printf ("c = % d/N", c );*/
} While (C! = 'N ');
Return 0;
}
Scanf ("% C", & C); why cannot this sentence normally receive characters? We use printf ("c = % d/N", c); Use int to represent C and enable printf ("c = % d/N", C ); in this sentence, let's see what the scanf () function assigns to C. The result is c = 10 and the ASCII value is 10? Line feed:/n. by the way, each time we press the "enter" key, we send a "Press ENTER" (/R) and a "line feed" (/N) to the keyboard buffer ), here/R is thrown away by the scanf () function, And/N is assigned to C by the scanf () function "incorrectly.
Solution: Add fflush (stdin) after two scanf () functions );
Function Name: fflush
Function: clears a stream.
Usage: int fflush (File * stream );
# Include <stdio. h>
Int main (void)
{
Int;
Char C;
Do
{
Scanf ("% d", & );
Fflush (stdin );
Scanf ("% C", & C );
Fflush (stdin );
Printf ("A = % d c = % C/N", a, c );
} While (C! = 'N ');
Return 0;
}
Here is an example of using a space character to process the residual information in the buffer zone:
Run the program with an error:
# Include <stdio. h>
Int main (void)
{
Int I;
Char J [10];
For (I = 0; I <10; I ++)
{
Scanf ("% C", & J [I]); * Note that there is no space before % */
}
J [10] = '/0 ';
For (I = 0; I <10; I ++)
{
Printf ("% C", J [I]);
}
Return 0;
}
After the space control character is used:
# Include <stdio. h>
Int main (void)
{
Int I;
Char J [10];
For (I = 0; I <10; I ++)
{
Scanf ("% C", & J [I]); * Note that there is a space before % */
}
J [10] = '/0 ';
For (I = 0; I <10; I ++)
{
Printf ("% C", J [I]);
}
Return 0;
}
You can run it to see how different the two programs are.
Question 3 How to Deal with the program deadlock or error caused by incorrect input of the scanf () function?
# Include <stdio. h>
Int main (void)
{
Int A, B, C;/* calculate a + B */
Scanf ("% d, % d", & A, & B );
C = A + B;
Printf ("% d + % d = % d", A, B, C );
Return 0;
}
If the value of A and B is entered correctly in the above program, there is no problem. However, you cannot ensure that the user can enter the correct value every time. Once the wrong type is entered, your program is not a deadlock, but an incorrect result. Haha, maybe everyone has encountered a problem, right?
Solution: When the scanf () function is successfully executed, the return value is the number of successfully read variables. That is to say, your scanf () function has several variables. If all the scanf () functions are read normally, it returns a few. But pay attention to another problem. If illegal data is input, there may be residual information in the keyboard buffer.
Correct routine:
# Include <stdio. h>
Int main (void)
{
Int A, B, C;/* calculate a + B */
While (scanf ("% d, % d", & A, & B )! = 2) fflush (stdin );
C = A + B;
Printf ("% d + % d = % d", A, B, C );
Return 0;
}