About the problem of using scanf to input characters

Source: Internet
Author: User

About the problem of using scanf to input characters

 

1. scanf can be used to enter one character:

Char;

Scanf ("% C ",
& A); // stop it &

If you want to enter two characters

Char A, B;

Scanf ("% C", & );

Scanf ("% C", & B );

Printf ("% C, % C \ n", a, B );

In this way, the input will encounter a problem. If you enter m and press enter n in the console, the input will not wait for you to enter N, and the output will be directly output, and there is no desired CHARACTER n in the output. The reason is that the program gives m to A and returns the carriage return to B. (Because the carriage return is also a character ). The following is a solution:

Char A, B;

Scanf ("% C", & );

Scanf ("% C", & B );

If (B = '\ n ')

{

Scanf ("% C", & B );

}

Printf ("% C, % C \ n", a, B );

Note: If the input number is not a character, the above problem is not encountered. The C language can accurately distinguish between the number and the carriage return. When a character is entered, the C language cannot tell whether the carriage return you enter is an input character or an Terminator. Therefore, it is used as the input character by default.

2. If you want to enter multiple characters, that is, a string.

Char A [6];

Scanf ("% s", a); // note that this location does not exist &

Printf ("% s", a); // you can also use puts ()

 

If you enter ABCDE and press enter, the output result is correct. If there is a space in the input content, that is, ABCD e press enter, the output is ABCD without E. The reason is that scanf regards space and carriage return as the end of the string.

The following is a solution: change to gets.

Char A [6];

Gets ();

Printf ("% s", a); // you can also use puts ()

Gets is a function specifically used for string input. It only uses the carriage return as the end of the string, so spaces can be normally input.

 

Note: If you want to enter multiple strings, it may be a problem to use gets. It is also the question of carriage return.

Char A [6];

Char B [6];

Gets ();

Gets (B );

Puts ();

Puts (B );

This program runs normally. Let's look at the following section:

Char A [6];

Char B [6];

Int C;

Gets ();

Scanf ("% d", & C );

Gets (B );

Puts ();

Printf ("% d", c );

Puts (B );

The program runs normally. Enter the first string and press enter, and then enter the number to press Enter. If you do not enter the second string, the program will output the program. Cause of analysis: the carriage return after the input integer affects the gets function of the second string. Although the gets function uses the carriage return as the input confirmation, this carriage return is not retained. Therefore, if you press enter directly, then it is considered that a string with no content is input, that is, an empty string. So the second character array actually gets '\ 0 '.

Solution:

You can use the processing method when entering multiple characters above, but note that here we use to determine whether it is '\ 0' rather than' \ n '.

Char A [6];

Char B [6];

Int C;

Gets ();

Scanf ("% d", & C );

Gets (B );

If (B [0] = '\ 0') // note that B [0] is used for determination, rather than B. The value of B is an integer representing the first address, the value of B must not be '\ 0'

{

Gets (B );

}

Puts ();

Printf ("% d", c );

Puts (B );

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.