C Language Input and Output Summary

Source: Internet
Author: User

C LanguageBasicInput and OutputFunctions:

Putchar (): Output A character constant in the variable to the display screen;
Getchar (); input a character constant from the keyboard. This constant is the value of this function;
Printf (); format and control all types of data on the keyboard to output to the display screen;
Scanf (); input various types of data from the keyboard and store the data in program variables;
Puts (): outputs a String constant in the array variable to the display screen;
Gets (): enter a String constant on the keyboard and place it in the array of the program.
Sscanf (); extracts various types of data from a string.

Putchar () and getchar () names are used to get a character from the input stream and output a character, which is relatively simple and will not be discussed more.
Example:
Char c = getchar ();
Putchar (C );

FormatInput and OutputScanf () and printf () are the most useful, so let's focus on them.

Printf ():
General format:
Printf ("format control". Output list );
Eg: printf ("A = % d, B = % F, c = % C/N", A, B, C );
1; format control.
Format control is a string enclosed in double quotes, also known as "Conversion Control string". It contains the following two parts.
Format description: it consists of "%" and format characters, such as % d, % F, and % C. It is used to convert the output data to the specified format, the format description always starts with "%.
Common Character: a character that needs to be output as is, or a character with special meanings, such as/N and/t.

2. Output list
The data to be output can also be an expression. If multiple variables or expressions need to be output in a function, separate them with commas.

Output of some special characters:
Single quotation marks, double quotation marks, and backslash output are preceded by escape characters "/"
For example: "/'", "/", "//"
% Of the output uses two % together, that is, printf ("% ");

Common formats are described as follows:
Format characters
D. Output signed integers in decimal form (positive numbers do not output symbols)
O outputs unsigned integers in octal form (no prefix o is output)
X outputs an unsigned integer in hexadecimal format (the ox prefix is not output)
U outputs an unsigned integer in decimal format
F outputs Single-precision real numbers in decimal form
Lf outputs double-precision real numbers in decimal form
E outputs Single and Double Precision Real Numbers in exponential form
G outputs Single and Double precision real numbers with a shorter output width in % F % E
C Outputs a single character
S output string

I would like to emphasize that F is the same as LF in many online articles, that is, F can be used regardless of Single-precision or double-precision floating-point numbers, but I have tested it on poj, it is true that F can be used to output double data. However, if F is used for reading and writing double data, LF is used.
Speaking of double, we recommend that you use double instead of float when using floating point numbers, because in many cases, the float precision is insufficient, leading to WA.

Special:
For 64-bit IntegersInput and OutputIn the C ++ environment (VC) on poj, the 64-bit integer is:
_ Int64 (note that the front of Int Is two underscores)
Input and OutputThe format is "% i64d ".
In the G ++ environment (Dev C ++), the 64-bit integer is
Long long
Input and OutputThe format is "% LLD ".

Output width

The minimum number of digits of the output is expressed by a decimal integer. Note: If the actual number of digits is greater than the defined width, the actual number of digits is output. If the actual number of digits is less than the defined width, spaces or 0 are supplemented.

Precision
The precision format character starts with "." and is followed by a decimal integer. It indicates the number of digits to be output. If the number is output, it indicates the number of digits to be output. If the actual number of digits is greater than the defined precision, the part that exceeds the limit is truncated.

Character in logo format
-The result is left aligned with a space on the right.
+ Space of the output symbol (positive or negative). The output value is a positive value with a space and a negative value with a negative value.

For example:
Double C = 24212345.24232;
Printf ("% 020.4"); indicates that the output is accurate to the fourth digit after the decimal point, and the output occupies 20 digits.

Scanf:

Many scanf usage methods correspond to printf, so we will not repeat them here.
Scanf is particularly useful for filtering out unwanted things.
Example:
For example, if the input date is yyyy-mm-dd, you can write it as follows:
Int year, moth, Day;
Scanf ("% d-% d", & year, & moth, & Day );
For example:
Scanf ("% 3d % * 3D % 2D", & M, & N); enter 113 118 69 and press enter (the system will assign 113 to m, and assign 69 to N, because "*" indicates that the corresponding data is skipped, "118" does not assign any variable)

Puts () is rarely used and can be replaced by printf.

Gets () is to get a string from the input stream and put it into the character array:
Char in [100];
Gets (in );

The most common error is the string input:
Can carry on the character, string inputs include:
Getchar (), scanf ("% C"); scanf ("% s"), gets ()

Getchar () and scanf ("% C") have the same functions.
Note that the two functions read the characters at the current position in the input stream,
For example:
Scanf ("% d", & N );
C = getchar ();
Assume that the input 67/(assuming "/" represents the carriage return), the first scanf reads an integer 67, and the current input stream is after 67, that is, pointing to the carriage return, therefore, the second getchar () reads a carriage return, that is, c = '/N '.

Similarly, gets () also reads a line of strings from the current position.
For example:
Scanf ("% d", & N );
Gets (STR );
The string in the character array is "/N ".
Therefore, after reading a non-string type with scanf, if you want to read characters or character arrays, use an additional getchar () to read the carriage return, if there is more than one carriage return followed by any extra space, use gets () to read it.

Different from the preceding, scanf ("% s") ignores spaces, carriage returns, and tabs. And uses spaces, carriage returns, and tabs as the character string ending signs.

This is often the case where the first line of input is an integer, and the first line of each line is a character to indicate an operation, followed by some data, such:
4
A 100 2
B 23
A 23 89
B 34
You need to be careful with such input. Do not read the character as a carriage return.
To prevent exceptions, I usually handle such input as follows:
Char model [2];
Scanf ("% d", & N );
For (...,...,...) {
Scanf ("% s", model );
If (model [0] = 'A '){
}
Else {
}
}

Sscanf ():
Sscanf () is often used to break down strings. It has very powerful functions, but many functions require the knowledge of regular expressions. So let's introduce the simplest usage. If you want to know more, find it online by yourself.
1.
Char STR [2, 100], str1 [100], str2 [100];
Gets (STR );
Sscanf (STR, "% S % s", str1, str2 );
Splits the entire line of string into two strings by space, tab, or carriage return.
2
Returns the string of the specified length. In the following example, a string with a maximum length of 4 bytes is obtained.
Sscanf ("123456", "% 4 s", STR );

For C ++Input and OutputI will not talk about it in detail, because the speed of CIN and cout is too slow and is not recommended. It is generally used only when it is a last resort.
For example, when you want to read a string to a string object, you can only use cin. another common problem is how to read a whole line of string into a string, this requires the Getline function.
Usage:
Getline (CIN, STR );
The first parameter is the standard input stream Cin, and the second parameter is the string object that receives the read data. There is a third parameter, which is the identifier of the Terminator, but it can be used by default, so don't worry.

Note the difference between Getline and CIN. Getline:
The usage of CIN. Getline is as follows:
Char STR [20];
Cin. getline (STR, 20); indicates that a string of up to 20 characters can be read into the STR array. Note that STR here is a character array, the STR above is a string object.

In addition, do not mix cout and printf. Because cout is buffered and printf is not, the output data order is disordered.

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.