Usage and practice of scanf () functions
DaizH
Summary:
This article describes the usage of scanf () functions based on ANSI, WIN 95, WIN NT, and common errors and countermeasures in actual use.
Key words:
Scanf ()
First, preamble
In the C / C section of the 9CBS Forum, I often see "The usage of scanf () functions, and the various mistakes that appear," the post, germination, I wrote this article. In the article, it is aware of its understanding and understanding in learning and programming, with specific examples, the purpose is to make the beginners can use the scanf () function correctly, less unnecessary detours.
Second, the usage of scanf () functions
The scanf () function is a formatting input function, which is read from the standard input device (keyboard).
Its call format is: scanf ("
The formatted string includes the following three types of different characters;
1, format the specifier:
The format specifier is basically the same as the format specifier in the Printf () function. But the usage of format strings in the printf () function has some distinction. Let's take a look at the table below.
Format character
Description
% D
Enter a decimal integer from the keyboard
% O
Enter an octal integer from the keyboard
% x
Enter hexadecimal integers from keyboard
% C
Enter a character from the keyboard
% s
Enter a string from the keyboard
% F
Enter a real number from the keyboard
% e
The same as% F
Additional format description character table
character
Description
L
Enter "long" data
Hide
Enter "short" data
M
Specify the width of the input data
*
Empty reading a data
2, blank characters: Blank characters will enable the scanf () function to ignore one or more blank characters in the input in the read operation.
3, non-blank characters: A non-empty character will cause the scanf () function to remove the same character as this non-blank character when reading.
The address table is the address of all variables that need to be read, not the variable itself. This is completely different from the Printf () function, paying special attention. The address of each variable is separated.
E.g:
#include
void main ()
{
INT I, J;
Printf ("i, j =? / n");
Scanf ("% D,% D", & i, & j);
}
The scanf () function in the above example reads an integer number, then remove the comma that is then entered, and finally read another intensive number. If "," this particular character is not found, the scanf () function terminates. If the separator between the parameters is space, one or more spaces must be entered between the parameters.
Description:
(1) For string array or string pointer variable, since the array name and pointer variable name itself is address, it is not necessary to add "&" operators in front of them using the ScanF () function.
E.g:
#include
void main ()
{
Char * P, STR [20];
P = new char [20];
Scanf ("% s", p); / * Enter string from Jiji * /
Scanf ("% s", STR);
Printf ("% s / n", p); / * Output string to the screen * /
Printf ("% S / N", STR);
}
(2) An integer can be added between the "%" format specified in the format string, indicating the maximum number of digits in any read operation. As in the above example, if you specify only 10 characters to give the string pointer P, the first SCANF () function statement becomes: scanf ("% 10s", P);
Once the program is run, once the number is input, the number is more than 10, and the P will no longer be read, and the next reading function is SCANF ("% s", STR) will begin from the 11th character.
(3) There is no precision control in the scanf () function.
Such as: scanf ("% 5.2f", & a); it is illegal. You cannot try to use this statement to enter a decimal number of 2 digits.
(4) The demand address is required in Scanf, if the variable name is given, it will be wrong.
Such as scanf ("% d", a); is illegal, it should be changed to SCNAF ("% D", & a); it is legal.
(5) When entering multiple numerical data, if there is no non-format character in the format control string, there is a space, Tab, or a carriage return.
C compiles the space, Tab, carriage return, or illegal data (if "12A" input "12A" is input to "% D", that is, it is considered to end.
(6) When entering the character data (% C), if there is no non-format character in the format control string, all the characters all entered are valid characters.
For example: SCANF ("% C% C% C", & A, & B, & C);
Enter is:
D e f
Then, the 'D' is given a, '(space)' gives B, 'E' gives C. Because% c requires a single character to be used as a space as a space, then the '' is given to b as the next character.
On only when the input is: DEF, the 'D' is assigned to a, 'E' gives C. If you join space in format control as a spacing,
Such as Scanf ("% C% C% C", & A, & B, & C); if the data can be added between the data.
We use some examples to explain some rules:
#include
void main ()
{
CHAR A, B;
Printf ("INPUT CHARACTER A, B / N");
Scanf ("% C% C", & A, & B); / * Note no symbols between two% C * /
Printf ("% C / N", A, B);
}
Since there is no space in the scanf function "% C% C", the input M n, the result is only M. When the input change is changed to the MN, the MN can be output, see the input operation below: Input Character A, B
Mn (your value you entered)
MN (value displayed on the screen)
#include
void main ()
{
CHAR A, B;
Printf ("INPUT CHARACTER A, B / N");
Scanf ("% C% C", & A, & B); / * Note that there is a space between two% C * /
Printf ("/ n% C / N", A, B);
This example indicates that there is space between the input data between the SCANF format control string "% C% C", and the input data can be spacer.
(7) If there is a non-format character in the format control string, it is also necessary to enter the non-format character. E.g:
Scanf ("% D,% D,% D", & A, & B, & C); where non-formatters ",", " The format must be the same)
Another example: scanf ("A =% D, B =% D, C =% D", & A, & B, & C);
Then the input should be a = 5, b = 6, c = 7
If the input data is inconsistent with the type of output, although the compilation can pass, the result will be incorrect.
#include
void main ()
{
Int a;
Printf ("INPUT A Number");
Scanf ("% d", & a);
Printf ("% ld", a);
}
Since the input data type is integer, the format string of the output statement is explained as a long integer, so the output result and the input data do not match. The output is not an input value.
If SCANF ("% D", & A); statement is changed to Scanf ("% ld", & a);
The input data is long integer, and the input and output data is equal.
Third, common mistakes and countermeasures
Question 1:
I started to learn the C procedure, so the question is very shallow, I hope you don't laugh. I have compiled a program myself, but the result of run is different from me.
#include
void main ()
{
Static Int A [2] [3] = {{1, 3, 4}, {7, 9, 6}};
INT I, J, K;
For (k = 1; k <= 2; k )
{Printf ("please input num:");
Scanf ("% D% D", & I, & J);
IF (i <2 && j <3)
Printf ("NUM =% D / N", A [I] [J]);
Else Printf ("Input Is Error, / N");
}
Printf ("Programm is Complete./N");
}
I want to change the 8th line to
Scanf ("i =% D j =% D", & i, & j);
Then the program is running
Please Input Num: i = 1 J = 2
Num = 6
Num = 6 (I originally hope to repeat the first line and let me enter)
Programm is Complete.
Why can't I enter the second time?
reply:
I use Turbo C 2.0 to confirm that there is a question you say. Like scanf ("i =% DJ =% D", & I, & J); this input method is more special, TC 2.0 obviously not clearly input buffer as normal after the first input, so that SCANF is performed second time When the program does not allow you to enter the result of the last input. If you have to do this, you should add it before scanf:
Fflush (stdin);
This clearly removes the keyboard buffer.
Question 2:
Why do SCANF ("% C", varname) must change to scanf ("% c") to receive characters correctly?
reply:
Similar to the topic, there must be a space in front of% C, otherwise the system reads the variable after entering other values before entering other values, resulting in a dead cycle. Of course, if Scanf ("% C", & varname) is the first read statement, you can do not need to space. Question 3: (Forget the address operator "&" when entering the variable
INT A, B;
Scanf ("% D% D", A, B);
reply:
This is not legal. The scanf function is to store the value of A and B in accordance with A and B's address in the memory. "& A" refers to the address in memory.
Question 4: (In the way the data is input, it does not match the requirements)
1SCANF ("% D% D", & A, & B);
When entering, you cannot use a comma to make a separator between two data, such as the input is not legal:
3,4
When entering the data, between one or more spaces between the two data, the Enter key can be used to jump.
2SCANF ("% D,% D", & A, & b);
C provides that if there are other characters in the "Format Control" string, there are other characters in addition to the format description, the same characters as these characters should be input when entering the data. The following input is legal:
3,4
There is no comma or other characters without commas.
3 4 3: 4
As another example:
Scanf ("A =% D, B =% D", & A, & B);
Enter should be as follows:
A = 3, b = 4
Question 5: (Input character format is inconsistent)
When entering characters in "% C" format, "space characters" and "escape characters" are input as a valid character.
Scanf ("% C% C% C", & C1, & C2, & C3);
Enter a b c
Character "A" Give C1, Character "" is given to C2, and the character "b" is given to C3 because% c requires a space as a space as two characters as needed.
Question 6 :(. The data type of the input and output is inconsistent with the format specifier used)
For example, a is defined as an integer, B is defined as a real shape
A = 3; b = 4.5;
Printf ("% f% D / N", A, b);
Do not give an error message when compiling, but the results will not be in conformity. This mistakes are particularly important.
Question 7: (When entering the data, attempt specified accuracy)
Scanf ("% 7.2f", & a);
This is not legal, and accuracy cannot be specified when entering data.
Question 8: (Add the address operator in the location where the address operator is not added)
Scanf ("% s", & str);
The process of the C language compilation system logs to the array name is: the array name represents the start address of the array, and the input item in the scanf function is the character number group name, and it is not necessary to add addresses &. Should be changed to: scanf ("% s", str);
Fourth, conclusion
This paper mainly tells the usage of scanf () functions in C / C , focusing on using common errors and countermeasures that occur during the process of SCANF (). Of course, some of the solutions in the text can be better resolved with other functions and methods, but this article only discusses the scanf () function itself. There are some shortcomings in the text, and readers are welcome to correct.