C language format input and output functions
- 2020-04-02 03:01:29
- OfStack
1. Format output function printf ()
1. The invocation form is generally: printf(" format control string ", output table column);
2. The format control string is used to specify the output format. It has three forms:
1. Format specifier: specifies the output format of the contents of the corresponding output table column, starting with %, such as %d, %o, etc
2. Escape character: to output the control code or special characters represented by escape character, such as' \n' and '\t'.
3. Ordinary characters: characters that need to be output as is.
3. The output table lists a number of data items that need to be output, which corresponds to the format specifier in terms of quantity and type.
4. Format character m specifies the width of the output data, n to the real number represents the output n decimal places, the number of characters intercepted on the string represents the number of characters, + represents the right alignment, usually omitted.
-- indicates left alignment. L is used for long integer data. It can be added before d, o, x, and u.
Format characters
|
The data object |
The output form
|
Data output method
|
% ( + ) -md |
int
unsigned int short unsigned short char |
Decimal integer |
1 And no m Output in actual digits
2 And there are m The output m A; More than m Bit, according to the actual number of output, not enough space
3 And there are + (the default is + ) align right (left padding)
4 And there are - Left alignment (right padding)
|
% ( + ) -mo
|
Octal integer
|
||
%(+)-mx
|
Hexadecimal integers
|
||
%(+)-mu
|
Unsigned integer
|
||
%(+)-mld
|
long unsigned long |
Decimal integer |
|
%(+)-mlo
|
Octal integer
|
||
%(+)-mlx
|
Hexadecimal integers
|
||
%(+)-mlu
|
Unsigned integer
|
||
%(+)-m.nf
|
float double
|
Decimal decimal
|
|
%(+)-m.ne
|
Decimal index
|
||
%(+)-g
|
Automatically choose %f and %e Short output widths for single - and double-precision floating - point Numbers |
||
%(+)-mc
|
char int short |
A single character
|
1 And no m Output single character 2 And there are m The output m Bit, fill space 3 And there are + (the default is + ) align right (left padding) 4 And there are - Left alignment (right padding) |
%(+)-m.ns
|
string
|
A string of characters
|
1. There is no m , n Output all characters as actual strings 2 And there are m , n Just before the output n Characters, space padding 3. There are + (the default is + ) align right (left padding) 4 And there are - Left alignment (right padding) |
Two: format input function scanf ()
1. The calling format is generally scanf (" format control string ", address table column);
2. The format control string has the same meaning as the printf () function, except that it controls the input format.
3, address table column is a number of data waiting for input of the corresponding memory location, separated by a comma, the general form is &a, a is a variable;
4. The address list in terms of quantity and type corresponds to the format specifier in the format control string.
5. The format character h represents the input short data, which can be used in front of d, o and x. M specifies the width of input data.
* means that the corresponding data item is not assigned to the corresponding variable after it is read in. More format specifiers and their combinations are shown below
Format characters
|
The data object
|
The input form
|
Data entry method
|
%md |
int short unsigned int unsigned short |
Decimal integer |
1 And no m Enter in actual digits
2 And there are m The input m Insufficient place, m Then follow the enter key |
%mo |
Octal integer
|
||
%mx |
Hexadecimal integers
|
||
%mld |
long unsigned long |
Decimal integer |
|
%mlo |
Octal integer
|
||
%mlx |
Hexadecimal integers
|
||
%mlf |
float double |
Decimal integer
|
|
%mle | |||
%mc |
char
|
A single character
|
1 And no m Take a single character 2 And there are m The input m Bit, just take the first character |
%ms |
string
|
A string of characters
|
1 And no m A number of characters before retrieving the car or space 2 And there are m Just before taking m character |
That's all for this article, I hope you enjoy it.