Principles of standard input: CIN and scanf

Source: Internet
Author: User

1. Processing Method of CIN


1) if CIN reads an integer, it will automatically ignore line breaks and spaces. When the end mark is reached, the number returned by CIN> A is false.


Int;
While (CIN> ){
Cout <A <Endl;
}
Return 0;
Eg: 1, 2, 3
1 23
1 A2
A 12
Note: When CIN reads the 'A' character, if it is CIN> (INT); then CIN returns false, and the integer in variable A is 0; if it is \ n \ r, it is automatically ignored;


2) When CIN reads the string, \ n is used as the delimiter of the string.
String;
While (CIN> ){
Cout <;
}
Note: spaces are treated as delimiters and will not be stored in a. In addition, writing "\ n" in the standard input has no effect on line breaks, in this way, two characters are stored, and the keyboard's return key is used to store the "\ n" character.


3) CIN reads the string char *
After reading, \ n is automatically converted to \ 0


4) if the file ends, false is returned.


2. scanf Processing Method


Understanding of standard input: by default, scanf reads characters from the standard input and converts the characters accordingly. If it is a space or line feed, it will be skipped directly. If it does not meet the formatting requirements, therefore, scanf does not process nor reduce the information in the buffer.


1) read INTEGER:
Int A = 12;
While (scanf ("% d", & )! = EOF ){
Cout <A <Endl;
}
So, if we enter 'A', it will be in an endless loop.


2) read integer scanf ("% d, % d", & A, & B) is feasible; but read string scanf ("% s, % s ") is not feasible


Understanding of the input process: the input process consists of two parts. The first part needs to be sent to the keyboard buffer, Which is buffered by line;
The second process is to read data from the buffer to the corresponding memory space according to the formatting requirements;
3) hybrid input


Int A = 12, B = 12;
Char c = 'C ';
Cout <scanf ("% d % C", & A, & B, & C );

If we enter "123 A1", the input fields 1, B, and C are not assigned values. That is to say, scanf cannot ensure that all subsequent parameters are correctly assigned values.


3. fgets Processing Method

First, both gets and fgets can handle string input problems, and both can enter spaces. The difference is that fgets does not convert \ n to \ 0.

Principles of standard input: CIN and scanf

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.