Some features of scanf and scanf
Scanf is very sensitive. It is best not to set the accuracy and field width in scanf; otherwise, the input may fail.
Example:
# Include <stdio. h>
Int main (void)
{
Int a = 0;
Scanf ("% 5d", & );
Printf ("a = % d", );
Return 0;
}
I run this Code. It is okay when the input integer is smaller than or equal to 5 digits. The output is correct. When the input integer is more than five digits, it reads only the fifth digit, instead of the remaining input. Are those input with more than five digits discarded? No, this is a feature of scanf. When scanf () fails to read the specified input, it leaves this incompatible input for the next read.
Modify the above Code
# Include <stdio. h>
Int main (void)
{
Int a = 0;
Int B = 0;
Scanf ("% 5d", & );
Printf ("a = % d \ n", );
Scanf ("% d", & B );
Printf ("B = % d \ n", B );
Return 0;
}
The running result is as follows:
If you do not pay attention to this feature, some problems may not be debugged in subsequent programming, so it is necessary to understand it.
Let's look at another example.
# Include <stdio. h>
Int main (void)
{
Int;
Int status = 0;
Printf ("Enter an integer :( q to quit )");
Status = scanf ("% d", & );
While (status = 1)
{
Printf ("Enter next integer :( q to quit )");
Status = scanf ("% d", & );
}
Printf ("Finished ");
Return 0;
}
The purpose of this example is to warn you not to use = where = should be used.
However, the preceding features of scanf can also be described. If you run this program, you will find that the program runs normally when you do not enter q. After you input q, the program starts to infinite loop and you have no chance to input more. WHY?
This is because the input q and status are set to 0, but the status is reset to 1 during loop judgment and another loop is started. BESIDES, the % d type to be read by scanf, that is, the integer type, while q is not an integer. Therefore, q is not read and remains in the buffer zone waiting for the next scanf to read. In the next loop, scanf fails to read the q left in the previous step, which is why the user cannot input the q again.
Next feature of scanf
Scanf ("% d", & a); this statement must be read into an integer and assigned to.
When the read value is not an integer
1. enter a blank character (tab, space, line break). scanf skips the blank character until the first non-blank character is entered. Then it reads the character until it encounters a blank character, or a character that does not conform to the type being read. Therefore, scanf ("% d", & a, & B); scanf ("% d", & a, & B ); scanf ("% d", & a, & B); behavior is the same
2. The input is a character that does not conform to the reading type, such as a letter. It will keep this incompatible character for the next scanf to read.
Floating Point Numbers are similar.
But for % c, the situation is slightly different.
For % c specifiers, all input characters are equal. If the next input character is a space or line break (whether it is entered this time or left behind), the space or line break will be assigned to the specified variable; no blank characters will be skipped.
But! Adding a space to a format string will lead to some differences.
Command scanf ("% c", & ch); read the first character encountered in the input, while scanf ("% c", & ch ); read the first non-blank character.
Note that the space is located before % c, not % c
This provides us with an idea to eat the linefeed in the buffer zone. In the past, if scanf ("% c", & ch); was used before scanf, we had to use fflush (stdin); or add a % * c to eat the line break. But now we have a new method that is easier to use. That is to replace scanf ("% c", & ch); with scanf ("% c", & ch );
The space in the format string indicates that any space before the next input item is skipped. Note that the concept of any space also includes special cases without space.
Scanf ("% d, % d", & a, & B); will accept any of the following line of input
88,21
88, 21
88, 21 note that the comma is an English Punctuation and must be followed by the first number.
Scanf ("% d, % d", & a, & B); will accept any of the following line of input
88,21
88, 21
88, 21
88,
21
---------------------------------------------------------------------------