Scanf () function Doubt (below)
I have expressed two views here, and I reiterated this time: 1. This article only discusses the Scanf () function control string, all routines of this article do not constitute program suggestions. 2. Everything has to be pro, different compilers of different platforms may have different results. All routines in this article are debugged under Win-TC Windows Me.
Fourth, SCANF () function control string common error and countermeasure skills
Question 1: The program is compiled, but the error prompts are as follows: scanf: floating point formats not linked Abnormal Program Termination
Error sample program:
#include
For (i = 0; i <3; i ) for (j = 0; j <3; j ) Printf ("% f", s [i] [j]);}
This is actually a problem that is not related to the topic of this article, but also is not related to the scanf () function, is a compiler.
The reason is clear: no link floating point library. Early system memory resources are tight, and the amount of multi-dimensional floating point array takes up the amount of memory (one-dimensional floating point array does not have this problem), so TC is trying to do not add a unrelated part when compiling. When not found a floating point conversion program, it is not This section is installed in the executable program. Sometimes TC does not correctly identify that it is actually really a floating point conversion, so it will appear above.
Solved method: Tell TC to do input conversion of floating point numbers. Add the following statement to the above program. There is / * here * /
Method 1: Float C; scanf ("% f", & c);
Method 2: Float C, * T; // This handle error, now correct & t === "* T;
T = & C; .....
That is, the compiler only has a floating point conversion, and TC will connect floating point conversion, so there is a floating point variable in general a larger program. But the problem is not over. I have a "one-dimensional floating point array without this question", then let's take a look at this way:
#include
For (i = 0; i <3; i ) for (j = 0; j <3; j ) Printf ("% 7.2f / n", s [i] [j]);}
This way we drop the multi-dimensional floating point array to a one-dimensional floating point array to process, debug it, and the program runs normally. This shows that the TC compiler only has this "unlink floating point library" in the process of processing the multi-dimensional float array (structure).
Question 2: SCANF () function does not accept a string with spaces? Such as: i love you! #Include
Return 0;}
Enter: I live you! Output: i
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.
Therefore, the above procedure does not meet the expected purpose, and the space behind the scanf () scanned to "i" is considered to end the assignment of the STR and ignores the "love you!" Here. Here, pay attention is "Love you!" Keyboard buffer (on this problem, I have seen online, but I have been debugging, in fact, the buffer string first pointer is equal, that is, the buffer is emptied, SCANF () function Should just scan the stdin stream, which is in stdin). We change the above program to verify:
#include
Enter: i love you! Output: i love you!
Ok, the reason is known, can the scanf () function can't complete this task? The answer is: can! Don't forget that the scanf () function has a% [] format control (if you don't know if you don't know about this article), please see the following procedure:
#include "stdio.h" int main () {char string [50]; / * scanf ("% s", string); no spaces * / scanf ("% [^ / n]", string; Printf ("% s / n", string); return 0;}
Question 3: Keyboard buffer residual information problem
#include
Do {Scanf ("% D", & a); scanf ("% c", & c); Printf ("A =% D C =% C / N", A, C); / * Printf ("c =% D / N ", c); * /} while (c! = 'n');} scanf ("% c ", & c); this sentence cannot receive characters normally, what is the reason? We use Printf ("C =% D / N", C); expand C with Int, enable Printf ("C =% D / N", C); this sentence, see the scanf () function assignment to C What is it, the result is c = 10, what is the ASCII value of 10? Retained / n. Right, each of us hit the "Enter" button, send it to the keyboard buffer (/ r), a "wrap" (/ n), here / R is Scranf ( The function is handled out (just think so, ^ _ ^), and / N is assigned to C by the scanf () function "error".
Solution: You can add fflush (stdin) after two scanf () functions; there is also getCH (); getchar (); can also, but to be placed as a specific scanf () statement plus that, this is not analyzed here. Readers to explore themselves. But add fflush (stdin); no matter what situation is feasible.
Function Name: FFLUSH Function: Clear a flow: int File * stream);
#include
Do {Scanf ("% D", & a); fflush (stdin); scanf ("% c", & c); fflush (stdin); Printf ("A =% D C =% C / N", A, C );
} while (c! = 'n');}
Here, give an example of processing buffer residual information with "Space]:
Run an error program:
#include
After using the space control:
#include
You can run what you do in seeing two programs.
Question 4 How to deal with the scanf () function error input causes the program deadlock or error?
#include
Scanf ("% D,% D", & A, & B); C = A B; Printf ("% D % D =% D", A, B, C);}
If the program is correct, if you correctly enter the value of A and B, then there is no problem, however, you can't guarantee that the user can enter correctly, once you entered the type of error, your program is not a dead lock, just get a wrong result Oh, this may have problems with everyone? Workaround: The return value when the scanf () function performs success is the number of variables successfully read, that is, your scanf () function has several variables, if the scanf () function is all normal, it returns a few . But here should also pay attention to another problem. If illegal data is entered, the keyboard buffer may also have a residual information issue.
The correct routine:
#include
While (Scanf ("% D,% D", & A, & B)! = 2) fflush (stdin); c = a b; Printf ("% D % D =% D", A, B, C); }
On this article, I finally saved several sentences, my own level is limited (indeed limited ^ _ ^, this is the truth), the fallacy is inevitable. It is said that the people will give one or two. Thank you. (Full text)
Knocker 2004.10.21