C scanf () stops receiving input and scanf (& amp; quot; % c & amp; quot;, & amp; c) eats carriage return or space characters.

Source: Internet
Author: User

C-language scanf () STOP accepting input and scanf (& quot; % c & quot;, & amp; c) eating carriage return or space characters
When the scanf () function receives input data, it ends a data input in the following circumstances: (instead of ending the scanf function, the scanf function only has data in each data field and ends after pressing enter ).
① In case of space, "enter", and "Skip" keys.
② End with width.
③ Illegal input. Question 2: Does the scanf () function correctly accept strings with spaces? For example, I love you!
# Include
Int main ()
{
Char str [80];

Scanf ("% s", str );
Printf ("% s", str); return 0;
} Input: I live you!
Output: When the I scanf () function receives input data, it ends the input of a data entry in the following circumstances: (instead of ending the scanf function, the scanf function only has data in each data field, and press Enter ).
① In case of space, "enter", and "Skip" keys.
② End with width.
③ Illegal input. Therefore, the above program cannot achieve the expected purpose. scanf () scans the space after "I" and considers the assignment of str to an end, and ignores the "love you! ". Note that" love you! "It is still in the keyboard buffer zone (I have seen this on the internet, but after debugging, I found that, in fact, the first and last pointers of the buffer string are equal, that is, the buffer zone is cleared, the scanf () function should only scan the stdin stream, and the residual information is in stdin ). Let's change the above program to verify it: # include
Int main ()
{
Char str [80];
Char str1 [80];
Char str2 [80];

Scanf ("% s", str);/* enter: I love you! */
Printf ("% s", str );
Sleep (5);/* Wait 5 seconds to tell you where the program is running */
Scanf ("% s", str1);/* you do not need to enter the two statements, but scan the keyboard disk buffer */
Scanf ("% s", str2);/* you do not need to enter the two statements, but scan the keyboard disk buffer */
Printf ("/n % s", str1 );
Printf ("/n % s", str2 );
Return 0;
} Input: I love you!
Output: I
Love
You! Okay, the reason is clear. Can the scanf () function complete this task? The answer is yes! Do not forget that the scanf () function has a % [] format controller (if you are not familiar with % [], please refer to the previous article). Please refer to the following program: # include "stdio. h"
Int main ()
{
Char string [50];

/* Scanf ("% s", string); cannot receive space characters */
Scanf ("% [^/n]", string );
Printf ("% s/n", string );
Return 0;
} Question 3: residual information of the Keyboard Buffer
# Include
Int main ()
{
Int;
Char c; do
{
Scanf ("% d", & );
Scanf ("% c", & c );
Printf ("a = % d c = % c/n", a, c );
/* Printf ("c = % d/n", c );*/
} While (c! = 'N ');
} Scanf ("% c", & c); why cannot this sentence normally receive characters? We use printf ("c = % d/n", c); Use int to represent C and enable printf ("c = % d/n", c ); in this sentence, let's see what the scanf () function assigns to C. The result is c = 10 and the ASCII value is 10? Line feed:/n. by the way, each time we press the "Enter" key, we send a "Press Enter" (/r) and a "line feed" (/n) to the keyboard buffer ), here/r is processed by the scanf () function (think so ^_^), And/n is assigned to c by the scanf () function "incorrectly. solution: Add fflush (stdin) after two scanf () functions, and getch (); getchar () when the statement is added, we will not analyze it here. Let's explore it by yourself. But add fflush (stdin); whatever the situation is feasible. Function Name: fflush
Function: clears a stream.
Usage: int fflush (FILE * stream); # include
Int main ()
{
Int;
Char c; do
{
Scanf ("% d", & );
Fflush (stdin );
Scanf ("% c", & c );
Fflush (stdin );
Printf ("a = % d c = % c/n", a, c);} while (c! = 'N ');
} Here is another example that uses a space character to process the residual information in the buffer zone: run the program that has an error: # include
Int main ()
{
Int I;
Char j;
For (I = 0; I <10; I ++)
{
Scanf ("% c", & j);/* There is no space before % */
}
} After the space control operator is used: # include
Int main ()
{
Int I;
Char j;
For (I = 0; I <10; I ++)
{
Scanf ("% c", & j);/* Note that there is a space before % */
}
} You can run it to see how different the two programs are. Question 4 how to deal with the program deadlock or error caused by incorrect input of the scanf () function? # Include
Int main ()
{
Int a, B, c;/* calculate a + B */scanf ("% d, % d", & a, & B );
C = a + B;
Printf ("% d + % d = % d", a, B, c );
} For the above program, if the values of a and B are entered correctly, there is no problem. However, you cannot ensure that the user can enter the correct value every time. Once the wrong type is entered, your program is not a deadlock, but an incorrect result. Haha, maybe everyone has encountered a problem, right? Solution: When the scanf () function is successfully executed, the return value is the number of successfully read variables. That is to say, your scanf () function has several variables. If all the scanf () functions are read normally, it returns a few. But pay attention to another problem. If illegal data is input, there may be residual information in the keyboard buffer. Correct routine: # include
Int main ()
{
Int a, B, c;/* calculate a + B */

While (scanf ("% d, % d", & a, & B )! = 2) fflush (stdin );
C = a + B;
Printf ("% d + % d = % d", a, B, c );
}

# Include
 
  
Void main () {int a; char B; printf ("input a integer \ n"); scanf ("% d", & ); // There is a method below to absorb the carriage return characters here. In the second method, I didn't understand who can change the code and tell me how to use printf ("input a char \ n "); scanf ("% c", & B); printf ("% d, % c \ n", a, B);}/* (1) Use getchar () clear (2) use "% 1 s" to limit (3) use space in the format string or "% * c" to "eat" (4) use function fflush (stdin) clear all remaining content */
 
# Include
 
  
Int main () {int a; char B; printf ("input a integer \ n"); scanf ("% d", & ); // There is a method below to absorb the carriage return characters here. In the second method, I didn't understand who can change the code and tell me how to use printf ("input a char \ n "); scanf ("% 1 s", & B); printf ("% d % c \ n", a, B); system ("pause"); return 0 ;}
 
Environment (dev c ++)

Test Results

We can see that the value of a is abnormal. Why? <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + CtXi06a4w7rNseDS68b309C52M + 5E + 9re9t6jt0mg91tajuw.vcd4kpha + crxa0rujuw.vcd4kpha + Signature = "1" class = "dp-cpp">

  • Char B;
  • Int;
    Change the declaration of these two variables. Haha, the teacher said before. But I don't feel like a solution. If there are other variables, will this method still harm others.

    Second:

    [Cpp]View plaincopy
    1. # Include
    2. Int main (){
    3. Int;
    4. Char B [2];
    5. Printf ("input a integer \ n ");
    6. Scanf ("% d", & );
    7. // There is a method below to absorb the carriage return characters here. The second method is not clear. Can someone change the code and tell me how to use the second method?
    8. Printf ("input a char \ n ");
    9. Scanf ("% 1 s", & B );
    10. Printf ("% d % c \ n", a, B [0]);
    11. System ("pause ");
    12. Return 0;
    13. }
      B is interpreted as the form of character creation. Haha, that's good ....

      Result:



  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.