Cin, Cin. Get, Cin. Getline; scanf, getchar; scanf, gets

Source: Internet
Author: User
1. Cin, Cin. Get, Cin. Getline

When CIN reads data, it ends with a space. When the space character is discarded, the input carriage return is complete, which is normal.

Cin. Get is used to read characters. The input carriage return ends, but the carriage return and space characters are not discarded.

Note: cin. get () and CIN. get (char) is equivalent, but cin. get (char *, INT) is used to read strings. It can accept space characters. When the carriage return ends, the character is read according to the specified length (length minus 1), and the last carriage return is discarded.

Cin. getline () and CIN. get (char *, INT) has the same usage. The only difference is cin. when the string input by get (char *, INT) is too long, it will not cause the CIN function error, and subsequent CIN operations will continue to be executed. However, the former will cause the CIN function error, and subsequent CIN operations will not be executed.

2. scanf, getchar read characters

Scanf ("% C", & Ch)

Ch = getchar ()

The two are the same. They can be read based on the number of variables using space characters and carriage return characters.

3. scanf, gets read string

Scanf ("% s", STR) encounters a space character and a carriage return character. A space character and a carriage return character cannot be entered.

Gets (STR) stops when a carriage return ends, but spaces are allowed in the string. Do not discard the space character.

1. Cin, Cin. Get, Cin. Getline

When CIN reads data, it ends with a space. When the space character is discarded, the input carriage return is complete, which is normal.

Cin. Get is used to read characters. The input carriage return ends, but the carriage return and space characters are not discarded.

Note: cin. get () and CIN. get (char) is equivalent, but cin. get (char *, INT) is used to read strings. It can accept space characters. When the carriage return ends, the character is read according to the specified length (length minus 1), and the last carriage return is discarded.

Cin. getline () and CIN. get (char *, INT) has the same usage. The only difference is cin. when the string input by get (char *, INT) is too long, it will not cause the CIN function error, and subsequent CIN operations will continue to be executed. However, the former will cause the CIN function error, and subsequent CIN operations will not be executed.

2. scanf, getchar read characters

Scanf ("% C", & Ch)

Ch = getchar ()

The two are the same. They can be read based on the number of variables using space characters and carriage return characters.

3. scanf, gets read string

Scanf ("% s", STR) encounters a space character and a carriage return character. A space character and a carriage return character cannot be entered.

Gets (STR) stops when a carriage return ends, but spaces are allowed in the string. Do not discard the space character.

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.