tags: C language scanf many Space %c \n
I once did a small project, implemented in c languageCommodity Information Management System
I think everyone has done something similar
This project inevitably needs to enter characters from the keyboard
Choose an option
Then enter level by level
First level:
1. Product information added
2. Product information inquiry
3. Exit the system
<Please enter options>:
second level:
1. Commodity price inquiry
2. Query the place of production
3. Exit to the previous level
<Please enter options>:
Let's take a lookFake code
The pseudo code isCode that is not code, to explain the logic of your code
Generally, when the project starts to build, it will be a good tool
scanf(“%c”) Enter the first level of options
...
scanf(“%c”) Enter the second level of options
When entering the first level of options
Your carriage return is also counted as a character, which is'\n'
There is one more character, it exists temporarilyBuffer zonein
When calling scanf for the second time, this carriage return character is used
So you can see that the scanf behind doesn't work anymore
AndOnly need to worry about this problem when the output format is %c
Other %s, %d, etc. don't have to worry about this problem!
the reason is simple
When I hit enter, I have knocked an extra'\n' into the buffer, but this'\n' is a character that can only be found by specifying the input format %c, so why don't worry about other input formats.
method 1:
getchar() method, but sometimes it can't be solved well
Method 2(recommend):
step one:
This function is used to clear the impact of the enter key cache, and add the definition of this function to the code
void safe_flush(FILE *fp)
{
int ch;
while( (ch = fgetc(fp)) != EOF && ch != '\n' ); //end of file
}
Step two:
Before the scanf statement, you can call the function, and the function parameters are passed into the standard input (keyboard (stdin)).
safe_flush(stdin); //Keyboard: standard input file
scanf("%c",&c);
scanfSmall eggs:
1) When specifying the format, do not have a newline (\n) character.
2) If you want to enter multiple parameters, separate them with a space to form good habits. For example: scanf("%d %f",a,b);
3) Scanf format output can only specify the output width, not the output precision. For example, the wrong way of writing: scanf ("%.2f", &Money),I explained this in the previous section;
Not easy to organize
Like the collection and follow~
Please see the next simple code Suddenly, it seems to input 1 + 2, and then output 1.000000 + 2.000000, but the test proofs, this method does not work, first in the case, if it is the problem, if ther...
C language scanf() function abnormal problem: When inputting char type, the carriage return character is swallowed, causing program bug. Reason: The scanf() input principle is that the system stores t...
@TOC Scanf ("% d", & a), scanf ("% s", & a), etc. cannot be accepted by spaces, Tab, Enter, etc. For the last carriage return, it will be saved in the input buffer, and the...
scanf% c input buffer '\ n' exist, are read% c, which can not enter. Solution one: Before scanf add a getchar () However inelegant, stupid. Solution two: fflush (stdin); Empty input buffer Similarly, ...
Reprinted from https://blog.csdn.net/ReaF_star/article/details/85064010?ops_request_misc=%25257B%252522request%25255Fid%252522%25253A%252522161093836216780264057204%252522%25252C%252522scm%252522%2525...
scanf () function is used to input data format, i.e. the format specified by the user from the keyboard data is read into the specified variable. The prototype is: int scanf (char * format [, argument...
First understand the scanf statement So let's first understand the scanf statement: The scanf() function is a format input function, that is, input data from a standard input device (keyboard) into a ...
#scanf() function Function: execute formatted input Usage: int scanf(char *format[,argument,…]); The scanf() function is a general-purpose terminal formatting input function. It reads the input...
scanf return value The test code is as follows: It can be concluded: 1. The scanf() function has a return value and is of int type. 2. The value returned by the scanf() function is: the number of vari...
Return value of C language scanf () function Code example: Return value is equal to the number of correct input, if one is not received, returns 0 Keyboard input 1 2 Return value: 2 num1= 1 num2= 2 Th...