C language format input and output functions, format input and output functions
I. Format output function printf ()
1. The call format is printf ("format control string", output table column );
2. The formatting control string is used to specify the output format. It has three forms:
- 1. format description: Specifies the output format of the content of the corresponding output table column, starting with %, such as % d, % o, etc.
- 2. Escape Character: it is used to output the control code or special characters represented by escape characters, such as common '\ n' and' \ t'
- 3. common characters: the characters that need to be output as is.
3. The output table is listed as a number of data items to be output. It corresponds to the number and type of format specifiers;
4. The format character m specifies the width of the output data. n pairs of real numbers indicate the output n decimal places. + pairs represent the number of truncated characters in the string, indicating the right alignment, which is usually omitted.
-Left alignment. l is used for long integer data. It can be added before d, o, x, and u. More format specifiers and their combinations are as follows:
Format characters |
Data Object |
Output Format |
Data Output Method |
% (+)-Md |
Int Unsigned int Short Unsigned short Char |
Decimal integer |
1. output by actual number of digits without m 2. m-bit output is available. If m-bit is exceeded, the output is based on the actual number of digits. 3. + (+ by default) right alignment (left space fill) 4. Align with-left (fill spaces on the right) |
% (+)-Mo |
Octal integer |
% (+)-Mx |
Hexadecimal integer |
% (+)-Mu |
Unsigned integer |
% (+)-Mld |
Long Unsigned long |
Decimal integer |
% (+)-Mlo |
Octal integer |
% (+)-Mlx |
Hexadecimal integer |
% (+)-Mlu |
Unsigned integer |
% (+)-M. nf |
Float Double |
Decimal decimal |
% (+)-M. ne |
Decimal Index |
% (+)-G |
The short output width in % f and % e is automatically selected for Single Output and double-precision floating point numbers. |
% (+)-Mc |
Char Int Short |
Single Character |
1. Output a single character without m 2. m-bit output with spaces filled 3. + (+ by default) right alignment (left space fill) 4. Align with-left (fill spaces on the right) |
% (+)-M. ns |
String |
One string of characters |
1. Output all characters based on the actual string without m and n 2. Only the first n characters are output for m and n, and spaces are supplemented. 3. There are + (+ by default) right alignment (left fill space) 4. Align with-left (fill spaces on the right) |
Ii. format input function scanf ()
1. Call format: scanf ("format control string", address table column );
2. The formatting control string and printf () function have similar meanings. The difference is that they control the input format;
3. The address table contains memory unit addresses corresponding to a number of data waiting for input, which are separated by commas (,). Generally, the format is & a, and a is a variable;
4. The number and type of the address column correspond to the format specifiers in the format control string;
5. The format character h indicates the input short data. It can be used before d, o, and x, and m indicates the width of the input data,
* Indicates that the corresponding data item is not assigned to the corresponding variable after it is read. More format specifiers and their combinations are as follows:
Format characters |
Data Object |
Input Format |
Data Input Method |
% Md |
Int Short Unsigned int Unsigned short |
Decimal integer |
1. Input by actual number of digits without m 2. m-bit input is available. If m-bit is not available, press Enter. |
% Mo |
Octal integer |
% Mx |
Hexadecimal integer |
% Mld |
Long Unsigned long |
Decimal integer |
% Mlo |
Octal integer |
% Mlx |
Hexadecimal integer |
% Mlf |
Float Double |
Decimal integer |
% Mle |
% Mc |
Char |
Single Character |
1. Take a single character without m 2. m is input, which is only the first character. |
% Ms |
String |
One string of characters |
1. There are no characters before m to retrieve the car or space 2. m only takes the first m characters |