Scanf () function Doubt (on)
First, preamble
The scanf () function is the second function encountered during the learning C language (the first function is Printf (), the "Hello, World" program in the C language process (the first function is Printf (), Brian W. Keninghan & Dennis M.Ritchie Basically, all the first example of all C language learners), so the scanf () function should be a function that C learner can be skilled, but there are many beginners can't use this function, in actual programming. Error uses a scanf () function, leading to the program to generate a certain error that cannot be run normally, so that the "scanf () function has bugs", "scanf () function is unused, etc. This article combines the author's problems in programming practice and the forum, but the author is limited (rookie-level), it is inevitable that there is a fallacy, but also hope that one will be directed. (Email: knocker.k@126.com) Only, the next two two articles describe the usage of the scanf () function in the C language, focusing on using common errors and countermeasures that occur during the SCANF () function. 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. The upper article, the configuration of the SCANF () function control string is described in detail. The next article, introduces the common errors and countermeasures techniques that appear in the SCANF () function control string using the actual routine.
Second, the control string of the scanf () function
Function Name: Scanf Features: Performing Format Input Usage: int Scanf (char * format [, argument, ...]);
The scanf () function is a universal terminal formatting input function, which is read from the standard input device (keyboard). You can read any natural type data and automatically convert the numerical value into an appropriate machine format.
Its call format is: scanf ("
The scanf () function returns the number of data items that successfully assigned values, and EOF is returned when an error.
Its control string consists of three types of characters:
1. Format specifier; 2. White character; 3. Non-blank characters;
(A) formatting the specifier
Format character description
% A reads a floating point value (only C99 effective)% a to read a character% D read into a decimal integer% i read into decimal, octal, hexadecimal integer% O read into an octal integer% x Into the hexadecimal integer% x to read a character% c to read a string% f Reading a floating point% f The same upper% E, the same maximum, the same maximum, the same, the upper% g, the upper% p, read into a pointer % u Reads an equal value character number% of unsigned decimal integers% N to this read value [] Scan Character Set %% Read% Symbol Additional Format Description Chart
Modification
L / L length modifier input "long" data H length modifier input "Short" data W Integrity Specify input data The width of the input data * the asterisk is empty to read a data HH, LL is the same as above H, L but only is valid for C99. (B) blank characters
Blank characters will make the scanf () function to one or more blank characters in the input in the read operation, and the blank character can be Space, Tab, Newline, and the like until the first non-empty composite appears.
(C) Non-blank characters
A non-empty character will cause the scanf () function to remove the same character as this non-blank character.
Note: SCANF () control string knowledge will introduce here (it should be completely ^ _ ^), if there is any missing next time. Next, the actual routine will be set forth one by one.
Third, the use of the SCANF () function control string
example 1.
#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 ↙ (inputs A, B, C)
3, 4, 5 (the value of the A, B, and C output by Printf)
(1) & a, & b, & c & C is the address operator, and the memory address of these three variables is obtained separately. (2) "% D% D% D" is to enter three values in the decay format. When entering, one or more spaces, tab keys, and auto keys can be separated by one or more spaces. The following is a legal input method: 1 3 □ □ 4 □□□□ 5↙ 2 3↙ 4 □ 5↙ 3 3 (Tab button) 4 ↙ 5↙
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 ↙ (inputs A, B, C)
or
3, □ 4, □ 5 ↙ (inputs A, B, C)
3, □□□ 4, □ 5 ↙ (input A, B, C) ... all legal, but "," must follow the numbers, such as: 3 □, 4, □ 5 ↙ 非 非 非, the program is wrong. (Solution and reason later)
Rethlean:
1, the variables in SACNF () must use the address.
INT A, B; SCANF ("% D% D", A, B); // Error SCANF ("% D% D", & A, & B);
2, SCANF () format control strings can use other non-empty characters, but these characters must be entered when entering.
Example: Scanf ("% D,% D", & A, & B); input: 3, 4 ↙ (comma) SCANF ("A =% D, b =% D) ", & A, & b); input: a = 3, b = 4 ↙ (" a = "," b = ", comma and"% D,% D "" a = "," b = "and comma Correspondingly) 3, when entering "% C", spaces and "escape characters" are active as a valid character.
Example: Scanf ("% C% C% C", & C1, & C2, & C3); input: a □ b □ C↙ Result: A → C1, □ → C2, B → C3 (the rest of the lost)
When the scanf () function receives input data, the following is the next input: (not ending the scanf function, the scanf function has data only in each data field, and pressing the Enter). 1 encounter, "Enter", "Jumping" button. 2 The end of the width. 3 illegally input.
The last article wrote here, the third section of the routine "copy" from a tutorial from the Internet (the reasons are two: One, you can play a lot less words. Second, □ I don't know how to play. ^ _ ^) And delete the mistakes. This is also a word, readers here: Everything has to be pro, even if it is a classic book, it is not exempt from omissivity, so the compiler is the most reliable, it is It is wrong to tell you if the compiler will tell you.