Collection of printf print format functions in c language
Usage:
int printf(const char *format,[argument]);
Format parameter output format, defined as:
% [logo] [width] [. PERC][F | N | h | l] type
Specify the data output mode as follows:
1.type has the following meanings:
D signed 10 decimal integer
I signed a decimal integer of 10.
O-sign octal integer
U unsigned 10 decimal integer
X is the unsigned number of 16, expressed in lowercase abcdef.
X is the unsigned number of 16, which is expressed in capital ABCDEF.
Floating point number
Floating-point Numbers in the Scientific Representation Format of E/e
G uses the total number of digits in the representations of %f and %e to represent the shortest, indicating that the floating-point number G has the same format as G, but is expressed as an exponent.
C single character
S string
% shows the percent sign itself.
P displays a pointer, and the near pointer is denoted as XXXX.
The far pointer is expressed as XXXX:YYYY.
The parameter of n connection should be a pointer, which stores the number of characters written.
2.flags specifies the output format and has the following values and meanings:
There is no right alignment, and the left side is filled with 0 and spaces.
-left-aligned and right-filled with spaces
+Add the symbol+or-before the number.
Spaces only show negative symbols.
# When type=c, s, d, i, u, it has no effect.
Type=o, x, x, and add' 0', "0x" and "0x" before the values respectively.
When type=e, e, f, always use the decimal point.
When type=g, g, except the numerical value is 0, the decimal point is always displayed. 3.width is used to control the width of displayed values. The values and meanings are as follows: n(n= 1, 2, 3 ...) is at least n bits wide, which is not enough to be filled with spaces.
The width of 0n(n= 1, 2, 3 ...) is at least n bits, which is not enough to fill the * grid with zeros on the left.
In the formula list, the next parameter, width 4.prec, is used to control the number of digits after the decimal point. Its value and meaning are as follows:
Not displayed at the default precision.
0 when type=d, I, o, u, x, it has no effect.
When type=e, e, f, the decimal point is not displayed.
Maximum number of digits when N(n= 1, 2, 3...)type = e, e, f.
Type= Other, indicating the maximum width of the display. *
In the format list, the next parameter is still the width.
5.F|N|h|l indicates whether the pointer is a far pointer or whether the integer is a long integer.
F far pointer
Near pointer
H short integer or single precision floating point number
L long integer or double-precision floating-point number
1. Universal format
Printf (format control, output table columns)
For example: printf("i=%d, ch=%c\n ",I, ch);
Description:
(1) "format control" is a string enclosed by double apostrophes, also called "conversion control string", which contains two kinds of information:
① Format Description: It consists of "%"and format characters, and its function is to convert the output data into the specified format for output.
② Ordinary characters, that is, characters that need to be output as they are.
(2) "output table columns" are some data that need to be output, which can be expressions.
(3) The general form of 3)printf function can be expressed as
Printf (parameter 1, parameter 2, ... parameter n)
The function outputs parameter 2 ~ parameter n according to the given format of parameter/kloc-0.
2. Format characters (9 kinds)
(1)d (or I) format characters. Used to output decimal integers, which can be used in the following ways:
①%d, output according to the actual length of integer data.
②%md, where m is the width of the specified output field. If the number of digits of data is less than m, the left end is filled with spaces; If it is greater than m, it is output according to the actual number of digits.
③%ld(%mld is also acceptable), and output long integer data.
For example: length a =123456;
printf("%ld ",a);
(2)o format characters, which output integers in the form of octal numbers. Format: %o, %mo, %lo, %mlo.
(3)x (or X) format characters, and output integers in the form of hexadecimal numbers. Format: %x, %mx, %lx, %mlx.
(4)u-format symbols are used to output unsigned data, that is, unsigned numbers, and the output is decimal numbers. Format: %u, %mu, %lu.
See: li4-3.c/* Output of unsigned data */
(5)c format characters, which are used to output a character. Format: %c, %mc is acceptable.
(6) Characters in S format are used to output character strings. Format: %s, %ms,% ms,% m.ns,% m.ns.
See: li4-5.c /* string output */
(7)f format symbol, which is used to output real numbers (including single precision and double precision) and output in decimal form. Format: %f, %m.nf,% m.nf.
Note: The significant digits of single-precision real numbers are generally 7 digits, and double-precision real numbers are 16 digits.
See: li4-6.c/* Effective digits when outputting single-precision real numbers */
Li4-7.c/* Number of effective digits when outputting double-precision real numbers */
Li4-8.c/* Specify decimal places when outputting real numbers */
(8)e (or E) format symbols, and output real numbers in exponential form. Format: %e, %m.ne,% m.ne.
(9)g (or G) format symbol, which is used to output real numbers. According to the size of the numerical value, automatically select F format or E format (choose the one with smaller width when outputting).
explain
(1) Except for X, E and G (expressed in uppercase letters), other format characters must be in lowercase letters;
(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) See the table below for the format character table.
Table 4. Characters in1printf format
Format character interpretation
D, I output signed decimal integers (positive numbers do not output symbols)
O Output integers in octal unsigned form (no leading zero output)
X, X outputs hexadecimal unsigned integer (leading symbol 0x is not output), X outputs lowercase hexadecimal numbers A ~ F, and X outputs uppercase letters.
U Output integers in unsigned decimal form.
C output in character form, only one character is output.
Output string of
F output single-precision and double-precision numbers in decimal form, and output 6 decimal places implicitly.
E, e outputs real numbers in exponential form.
G, g Select the format with shorter output width in %f or %e format, and do not output meaningless 0.
Table 4.2 Additional format description characters of printf
Character; Role; letter
explain
Letter l
Used for long integers, which can be added before the format characters d, o, x and u.
M (for positive integer)
Minimum data width
N (for positive integer)
For real numbers, it means outputting n decimal places; For strings, indicates the number of characters intercepted.
-
The output number or character is tilted to the left in the field.
Program example: # include
int main()
{
printf("Hello,world \ n ");
while( 1);
}
# include & ltstdio.h & gt
int main()
{
int i = 1,j = 2;
printf("%d %d\n ",I,j);
while( 1);
}
Printf command purpose
Write print format.
grammar
Printf format [parameters ...]
describe
The printf command converts, formats, and writes the Argument parameter to standard output. The Argument parameter is formatted by the format parameter. The print format line cannot exceed the length of LINE_MAX bytes.
The following environment variables affect the execution of the printf command:
When LC_ALL and the corresponding environment variables (starting with LC_) do not specify a language environment, determine the language environment used for language environment cataloging.
LC_ALL determines the locale used to override any locale directory value set by LANG or any other LC_ environment variable.
LC_CTYPE determines the language environment in which the text byte data sequence is interpreted as characters; For example, one byte corresponds to a parameter of a multibyte character.
LC_MESSAGES determines the writing language of messages.
LC_NUMERIC determines the language environment of the number format. This environment variable affects the format of numbers written with E, E, F, G and G conversion characters.
The format parameter is a string containing three object types:
* Unformatted characters are copied to the output stream.
* Convert specifications, and each specification will result in 0 or more items in the list of retrieved value parameters.
* The following escape sequence. When copied to the output stream, these sequences cause their related operations to be displayed on devices with this function:
\ \ backslash
\ A warning
\b backspace key
\f Change page
\n newline character
\r input.
\t tab
\v vertical tabs
\ddd ddd is 1, 2-or 3-digit octal number. These escape sequences are displayed as bytes, and their values are specified by octal numbers.
The Argument parameter is a list of one or more strings written to the standard output under the control of the Format parameter.
If you need to meet the requirements of the Argument parameter, the Format parameter will be reused frequently. Any additional C or S conversion specification will be considered as providing an empty string parameter; Other additional conversion specifications will be considered as providing 0 parameters. The Format parameter here does not contain the conversion specification, only the Argument parameter appears, and the result is uncertain.
The conversion specification in each format parameter has the following syntax:
1.% (percent sign).
2. Zero or more options to modify the meaning of the conversion specification. Option characters and their meanings are as follows:
-The conversion result is left-aligned in the field.
+sign conversion results usually start with a sign (+or-).
Spaces If the first character of symbol conversion is not a symbol, the result will be prefixed with spaces. If spaces and+option characters are displayed at the same time, the space option characters will be ignored.
# This option specifies that the value is converted to an alternate format. For c, d, i, u and s transformations, this option has no effect. For O conversion, forcing the first digit of the result to be a, and 0 (zero) will improve the accuracy. For x and x transformations, non-zero results are prefixed with 0X or 0x, respectively. For E, E, F, G and G transformations, the result usually contains radix characters, even if there is no number after the radix characters. For G and G transformations, the ending zero will not be deleted as usual.
0 For D, I, O, U, X, E, E, F, G, and G transformations, leading zeros (followed by symbols or radix) are used to fill the field width, not spaces. If the 0 (zero) and-(minus sign) options are displayed, the 0 (zero) option is ignored. For D, I, O, U, X and X transformations, if precision is specified, the 0 (zero) option will be ignored.
note:
Other transformations do not define their behavior.
3. An optional decimal digit string that specifies the width of the minimum field. If the converted value is less than the field width, the field will be filled with the specified field width from left to right. If the left adjustment option is specified, the field will be filled in on the right. If the conversion result is wider than the width of the field, the field will be expanded to include the converted result. No truncation will occur. However, small precision may lead to right truncation.
4. Optional precision. Accuracy is a series of decimal numbers followed by. (point). If no precision is given, it is considered as 0 (zero). Accuracy specification:
* d, O, I, U, X or the minimum number of digits displayed by X conversion.
* The minimum number of digits displayed after the cardinality characters of E and F conversion.
* Maximum significant digits of G conversion.
* The maximum number of printed bytes of a string in S conversion.
5. Characters indicating the type of transformation to be applied, such as:
% do not convert. Print a% (percent sign).
D, I accept integer values and convert them into signed decimal notation. Precision Specifies the minimum number of digits displayed. If the value can be represented by fewer numbers after conversion, the leading zero extension will be used. The default precision is 1. The result of zero value conversion with zero precision is an empty string. Specifying a field width of zero as a leading character will cause the field width value to be padded with leading zeros.
O accepts an integer value and converts it to signed octal notation. Precision Specifies the minimum number of digits displayed. If the value can be represented by fewer numbers after conversion, the leading zero extension will be used. The default precision is 1. The result of zero value conversion with zero precision is an empty string. Specifying a field width of zero as a leading character will cause the field width value to be padded with leading zeros. Do not use octal values to represent field widths.
U accepts integer values and converts them to unsigned decimal notation. Precision Specifies the minimum number of digits displayed. If the value can be represented by fewer numbers after conversion, the leading zero extension will be used. The default precision is 1. The result of zero value conversion with zero precision is an empty string. Specifying a field width of zero as a leading character will cause the field width value to be padded with leading zeros.
X, x accepts integer values and converts them into hexadecimal representation. The letter ABCDEF is used for x conversion, and the letter abcdef is used for x conversion. Precision Specifies the minimum number of digits displayed. If the value can be represented by fewer numbers after conversion, the leading zero extension will be used. The default precision is 1. The result of zero value conversion with zero precision is an empty string. Specifying a field width of zero as a leading character will cause the field width value to be padded with leading zeros.
F accepts floating-point or double-precision values and converts them into decimal notation in [-] ddd.ddd format. The number of digits after the radix character (shown here as decimal point) is equal to the specified precision. The LC_NUMERIC locale directory determines the basic characters used in this format. If no precision is specified, six numbers are output. If the precision is 0 (zero), the radix character will not be displayed.
E, e accepts floating-point or double-precision values and converts them into exponential representation [-] d.dde{+|-}dd. The radix character is preceded by a number (shown here as a decimal point), and the number of digits after the radix character is equal to the specified precision. The LC_NUMERIC locale directory determines the basic characters used in this format. If no precision is specified, six numbers are output. If the precision is 0 (zero), the radix character will not be displayed. The number generated by the e conversion character is preceded by e instead of e. An index usually contains at least two numbers. However, if the index value to be printed exceeds two numbers, additional index numbers need to be printed if necessary.
G and G accept floating-point and double-precision values and convert them into the style of F or E conversion characters (or E in the case of G conversion), and specify the precision of the significant digits. Trailing zeros will be removed from the results. Cardinal characters are displayed only when followed by numbers. The style used depends on the converted value. Only when the exponential result of the conversion is less than -4, or greater than or equal to the accuracy, can the pattern g be used.
C accepts a value in the form of a string and prints the first character in the string.
S accepts a value in the form of a string and prints the characters in the string until the end of the string or the number of characters indicated by the precision is reached. If no precision is specified, all characters are printed until the first empty character appears.
B accepts values as strings, which may contain backslash escape sequences. Print the bytes in the converted string until the end of the string or the number of bytes indicated by the precision specification is reached. If no precision is specified, all bytes are printed until the first empty character appears.
The following backslash escape sequences are supported:
* The backslash escape sequence listed above is described in the format parameter. These escape sequences will be converted into the single characters they represent.
* \c (backslash c) sequence, which does not display and makes printf command ignore all remaining strings, all remaining string parameters and all additional characters contained in string parameters in format parameters.
Exit state
This command returns the following export values:
0 completed successfully.
An error occurred in>0.
example
1. Enter the following command:
printf ("%5d%4d\n ", 12 13,43);
Produce the following output:
_ 12 13_ _43
Use the Format parameter to print all the given strings three times. 0 (zero) is provided by the printf command to meet the final %4d conversion specification.
2. Enter the following command
printf ("%c %c\n ",78,79);
Produce the following output:
Nitrous oxide
document
/usr/bin/printf contains the printf command.
Copy quantity ~ ~ ~