Format specifies the output format, followed by the variable to be output
Currently, printf supports the following formats:
% C Single Character
% D Decimal integer
% F Decimal floating point number
% O Octal
% S String
% U Unsigned decimal number
% X Hexadecimal number
% Output percent sign %
A format description can contain several modifiers to specify the display width, decimal number, and left alignment:
-Left aligned
+Add '+' or '-' before a signed number
0The field width is filled with leading zeros instead of blank spaces.
The field width is an integer that specifies the minimum field for printing a formatted string. The precision is represented by a number after the decimal point. The number of characters to be output for each conversion specifier is displayed.
Note: The display with modifiers may be abnormal.
the data format description starts with "%". The format is %< data output width description> , data format description: When you need to output the result of a variable or a numeric value, the number of data records exactly corresponds to the number of output columns.
If the actual data is less than the width, then, the Left or Right spaces are filled based on whether the width is greater than zero. If the actual data is greater than the width, the output is based on the actual number of digits. If the default width is specified, the output is based on the actual width.
cstring. format ('% 10f', );
10 indicates that A is formatted with 10 characters in width.
% F indicates that the decimal part is 6 digits;
example
% 10f if part of the integer digit of A + decimal point (1 digit) + If the decimal part is greater than or equal to 10 digits, It is formatted as;
If a's integer digits + decimal point (1 digit) + decimal digits are less than 10, fill in a space before and fill in 10 digits;
example
%. 7f then
If the decimal part of A is greater than 7, the system truncates 7 decimal places, the integer part remains unchanged.
if the number of digits in the fractional part of A is less than or equal to 7, the digits below 7 are supplemented with 0;
Str. format (_ T ('left-justified INTEGER: %. 6D \ n'), B);
%. 6D
If the integer B is less than 6 digits, the value is 0 at the top;
If the integer B is greater than or equal to 6 digits, the form remains unchanged;
% 6D
If the integer B is less than 6 bits, a space is added before the maximum bits, yes. The format is 6 Characters in width.
If the integer B is greater than or equal to 6 characters, the format remains unchanged.
Appendix: printf
Int printf (const char * format, [argument]);
Format:
% [Flags] [width] [. Perc] [f | n | H | L] Type
Specify the data output method as follows:
1. The meaning of type is as follows:
D signed 10-digit integer
I signed 10-digit integer
O signed octal integer
U unsigned 10-digit integer
X/x signed hexadecimal integer
F/F floating point number
E/E floating point in scientific format
G uses the more precise characters in % F and % E to indicate that the floating point G is in the same g format, but is expressed as an index.
C Single Character
S string
% Display percentage itself
P shows a pointer, and the near pointer is expressed as: xxxx
Far pointer: xxxx: yyyy
N connection parameters should be a pointer, where the number of written characters is stored
2. Flags specifies the output format. The values and meanings are as follows:
No right alignment. Fill in 0 and space on the left.
+ Align left with spaces on the right
-Add the symbol + or-before the number-
A space is used to display symbols only for negative numbers.
# No effect on type = C, S, D, I, u
Type = O, X, X, add '0' before the value
Type = E, E, F, always use the decimal point
Type = G, G, in addition to the value 0, always show the decimal point 3. width is used to control the width of the displayed value. The values and meanings are as follows: n (n = 1, 2, 3 ...) the width must be at least N bits, and should not be filled with spaces.
0n (n =, 3...) must be at least n characters in width. If not, fill the space with 0 characters on the left. *
In the formula list, the next parameter is still width 4. prec, which is used to control the digits after the decimal point. The values and meanings are as follows:
No display by default
0 when type = D, I, O, U, X, no effect
Type = E, E, F, do not display decimal point
N (n = 1, 2, 3...) the maximum number of decimal places when type = E, E, F
Type = others, indicating the maximum width of the display .*
In the format list, the next parameter is still width.
5. f | n | H | L indicates whether the pointer is a remote pointer or an integer is a long integer.
F remote pointer
N near pointer
H short integer or single-precision floating point number
L long integer or double-precision floating point number
1. General Format
Printf (format control, output table column)
For example: printf ("I = % d, CH = % C \ n", I, CH );
Note:
(1) "format control" is a string enclosed by a dual-marker, also known as "Conversion Control string". It contains two types of information:
① Format description: it consists of "%" and format characters. It is used to convert the output data to the specified format output.
② Common character, that is, the character that needs to be output as is.
(2) "output table column" is the data to be output, which can be an expression.
(3) The general form of the printf function can be expressed
Printf (parameter 1, parameter 2 ,......, Parameter n)
The function is to set parameters 2 ~ Parameter n is output in the format specified by parameter 1.
2. format characters (9 types)
(1) D (or I) format. It is used to output a decimal integer. It can be used in the following ways:
① % D, output according to the actual length of integer data.
② % MD, M is the width of the specified output field. If the number of digits of the data is smaller than m, spaces are filled on the left side. If the number is greater than m, the data is output based on the actual number of digits.
③ % LD (% MLD), which outputs long integer data.
Example: Long A = 123456;
Printf ("% lD", );
(2) o Format operator, which outputs integers in the form of Octal numbers. Format: % O, % Mo, % Lo, % MLO.
(3) x (or X) format operator, which outputs integers in hexadecimal format. Format: % x, % MX, % lx, % mlx.
(4) U format, used to output unsigned data, that is, the number of unsigned data, in decimal format. Format: % u, % Mu, % lu.
See: li4-3.c/* output of unsigned data */
(5) The C format character, used to output a character. Format: % C, % MC.
(6) s format character, used to output a string. Format: % s, % ms, %-MS, % m. NS, %-M. ns.
See: li4-5.c/* string output */
(7) F format character, used to output real numbers (including Single and Double Precision), in decimal form. Format: % F, % m. NF, %-M. NF.
Note: The valid digits of Single-precision real numbers are generally 7 bits and the double precision is 16 bits.
See: li4-6.c/* Number of valid digits in the output of Single-Precision Real Number */
Li4-7.c/* Number of valid digits when double precision real numbers are output */
Li4-8.c/* specify decimal places when outputting real numbers */
(8) E (or E) format operator, which outputs real numbers in exponential form. Format: % E, % m. Ne, %-M. ne.
(9) g (or g) format character, used to output real numbers, according to the value size, automatically select the F format or eformat (select a type with smaller output width ).
3. Description
(1) Except for X, E, and g (in uppercase letters), other characters must be in lowercase;
(2) The "format control" string can contain escape characters;
(3) If you want to output the character "%", you should use two consecutive % in the "format control" string, for example:
Printf ("% F %", 1.0/3 );
(4) For details about the format, see the following table.
Table 4.1 printf format characters
Description of format characters
D, I outputs integers in a signed decimal form (positive numbers do not output symbols)
O outputs integers in octal unsigned form (0 is not output)
X and X output integers in the hexadecimal notation (the leading character 0x is not output), and X is used to output A ~ of the hexadecimal number ~ F is output in lowercase, and X is output in uppercase letters.
U outputs integers in unsigned decimal form
C is output as a character and only one character is output.
S output string
F outputs Single and Double Precision in decimal form, and outputs 6 decimal places implicitly.
E, E outputs real numbers in exponential form
G and G use a format with a shorter output width in the % f or % eformat without outputting meaningless 0
Table 4.2 Additional printf format description characters
Character
Description
Letter L
Used as a long integer. It can be added before the format characters D, O, X, and U.
M (representing a positive integer)
Minimum data width
N (representing a positive integer)
Returns n decimal places to the real number. Returns n decimal places to the string, indicating the number of truncated characters.
-
The number or character output is left-aligned in the domain.