SCANF is very sensitive, it is best not to do scanf under the precision, field wide setting, otherwise the input may be invalid.
Cases:
#include <stdio.h>
int main (void)
{
int a = 0;
scanf ("%5d", &a);
printf ("A =%d", a);
return 0;
}
I run this code, when the input integer is less than or equal to 5 bits, no problem, the output is correct. When the input integer is more than 5 bits, it will only read to the fifth bit, not the remaining input. So are those more than five bits of input discarded? No, this is another feature of scanf. when scanf () fails to read input of the specified form, 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", &a);
printf ("A =%d\n", a);
scanf ("%d", &b);
printf ("B =%d\n", b);
return 0;
}
The operation results are as follows
If this feature does not pay attention, it may be in the subsequent programming encountered some problems debugging, so it is necessary to understand.
Look at one more example.
#include <stdio.h>
int main (void)
{
int A;
int status = 0;
printf ("Enter an integer: (Q to quit)");
Status = scanf ("%d", &a);
while (status = 1)
{
printf ("Enter Next integer: (q to quit)");
Status = scanf ("%d", &a);
}
printf ("Finished");
return 0;
}
This example is intended to warn you not to use = = in places where you should use = =.
However, the above characteristics of scanf can also be described. Running the program will find that the program does not enter the Q when the normal operation, after entering Q, the program begins an infinite loop, the user does not have the opportunity to do more input. Why?
This is because the input q,status is set to 0, but when the loop is judged it resets the status to 1 and starts another loop. BESIDES,SCANF want to read the%d type, that is, the integer, and q is not an integral type, so Q is not read, leaving the buffer waiting for the next scanf to read. And when the next loop is read, the q,scanf that is left behind fails again , which is why the user can no longer enter it.
scanf next Feature
scanf ("%d", &a); The statement reads in an integer and assigns it to a.
When a non-integer is read, the following conditions are in place
1. The input is a blank character (tab, space, line break), scanf skips the whitespace character until the first non-whitespace, and then it reads the character until it encounters a white space character, or encounters a character that does not conform to the type being read. So scanf ("%d%d", &a,&b); scanf ("%d%d", &a,&b); scanf ("%d%d", &a,&b), the behavior is the same
2. Enter a character that does not conform to the type being read, such as a letter, which retains this incompatible character and is left for the next scanf to read in.
Floating-point numbers are similar.
For%c, however, the situation is slightly different.
Because for the%c specifier, all the input characters are equal. If the next input character is a space or line break (whether it is the input or the previous one), the space or newline will be assigned to the specified variable, and no whitespace characters are skipped.
But! Adding a space to the format string will cause some differences.
The command scanf ("%c", &ch) reads the first character encountered in the input, while scanf ("%c", &ch) reads the first non-whitespace character encountered.
Note that the position of the space is before the whole of%c, not% c
This gives us a way to eat buffer line breaks. Previously if in scanf ("%c", &ch), before scanf, we had to use Fflush (stdin), or add a%*c to eat the newline character. But now, we have a new approach, and it's easier. That is in the scanf ("%c", &ch), replacing the scanf ("%c", &ch);
Whitespace in a format string means skipping any spaces before the next entry, and note that the concept of any space also includes special cases where there are no spaces.
scanf ("%d,%d", &a,&b), will accept any of the following line inputs
88,21
88, 21
88, 21 note the comma is English punctuation, and the comma is followed by the first digit
scanf ("%d,%d", &a,&b), will accept any of the following line inputs
88,21
88, 21
88, 21
88,
21st
———————————————————————————————————————————————————————————————————————————
Some features of scanf