8.4.25 printf command (converts form arguments according to the form and outputs the results to the standard output)
Syntax
printf form [form argument ...]
Description
This command converts form arguments according to the form and outputs the results to the standard output.
Arguments
Form
The form is composed of the following three types of character strings:
-
Characters to be output to the standard output
-
Conversion specification
-
Escape characters
To specify a conversion, specify %, which indicates a conversion specification, followed in sequence by: flag characters, the minimum field width, the precision, and a conversion specifier. Flag characters, minimum field width, and precision can be omitted.
-
Conversion specification
%
Indicates a conversion specification.
-
Flag character string
You can specify the flag characters listed below, after the % sign that indicates a conversion specification. Specification of this item can be omitted.
Flag character
Meaning
-
The conversion result is left-aligned in the field.
If this flag character is omitted, the conversion result is aligned to the right.
+
Adds the + or - sign to the result of a signed conversion.
If this flag character is omitted, the - is added only when the conversion result is a negative value.
Space
Adds a space before positive numerical values in the result of a signed conversion.
If this flag character and the + flag character are both specified, the + flag character takes priority.
#
When any of the following conversion specifiers are specified, the conversion result is output in another format:
-
For an o conversion, 0 is added to the beginning of the conversion result (excluding cases when the conversion result is 0).
-
For an x or X conversion, 0x or 0X is added to the beginning of the conversion result (excluding cases when the conversion result is 0).
-
For an e, E, f, g, or G conversion, a decimal point is added to the output result without exception, even when a value that does not contain a decimal point is specified.
-
For a g or G conversion, the 0 at the end of the conversion result is not deleted.
0
For a d, i, o, u, x, X, e, E, f, g, or G conversion, spaces on the left are padded with zeros in the conversion result field.
If this flag character is specified together with the - flag character or the precision of one of the following conversion specifiers, the 0 flag character is ignored: d, i,o, u, x, and X.
-
-
Minimum field width
Specify the minimum field width by using a decimal number. You can specify the minimum field width from a range of 0 to 2147483647. Specification of this item can be omitted. If the number of characters of the value after conversion is less than the width of the field, spaces on the left are padded with spaces. However, if the left-alignment flag character is specified, spaces on the right are padded with spaces. If the size of the conversion result is larger than the field width, the field width is extended to fit the conversion result. If an asterisk (*) is specified, the value specified for the form argument is used as the minimum field width.
-
Precision
Specify the precision by using a period (.) followed by a decimal number. In UNIX, specify from a range of 0 to 2147483647. In Windows, specify from a range of 0 to 512. Specification of this item can be omitted. If only a period (.) is specified, 0 is assumed to be specified. For a d, i, o, u, x or X conversion, specify the minimum number of digits to be displayed. For an e, E, or f conversion, specify the number of digits to be displayed after the decimal point. For a g or G conversion, specify the maximum number of valid digits. For a s conversion, specify the maximum number of bytes of the character string to be displayed. If you specify an asterisk (*) instead of a value, the value specified for form argument is used.
-
Conversion specifiers
Conversion specifier
Meaning
d, i
Converts an argument to a signed decimal value.
Precision specifies the minimum number of digits to be displayed. If the number of digits of the value after conversion is less than the number of digits specified for precision, one or more 0s are added to the beginning of the value. The default precision is 1.
If you specify 0 for precision and then convert the value 0, an empty character string is output.
o
Converts an argument to an octal number without a sign.
Precision specifies the minimum number of digits to be displayed. If the number of digits of the value after conversion is less than the number of digits specified for precision, one or more 0s are added to the beginning of the value. The default precision is 1.
If you specify 0 for precision and then convert the value 0, an empty character string is output.
u
Converts an argument to a decimal value without a sign.
Precision specifies the minimum number of digits to be displayed. If the number of digits of the value after conversion is less than the number of digits specified for precision, one or more 0s are added to the beginning of the value. The default precision is 1.
If you specify 0 for precision and then convert the value 0, an empty character string is output.
x, X
Converts an argument to a hexadecimal value. If you specify x, lowercase characters (abcdef) are used. If you specify X, uppercase characters (ABCDEF) are used.
Precision specifies the minimum number of digits to be displayed. If the number of digits of the value after conversion is less than the number of digits specified for precision,
one or more 0s are added to the beginning of the value. The default precision is 1.
If you specify 0 for precision and then convert the value 0, an empty character string is output.
e, E
Converts a floating point number to a decimal number in the form [-]d.dddde± dd[d].
If you specify e, lowercase characters will be used. If you specify E, uppercase characters will be used.
One digit before the decimal point is displayed. If precision is not specified, 6 digits after the decimal point are displayed. If precision is specified, the specified number of digits after the decimal point is displayed. Also, if 0 is specified for precision, digits after the decimal point are not output.
f
Converts a floating point number to a decimal number in the form [-]dddd.dddd.
One or more digits before the decimal point are displayed. If precision is not specified, 6 digits after the decimal point are displayed. If precision is specified, the specified number of digits after the decimal point is displayed.
g, G
Displays a floating point number in format of f or in the format of e and E.
The displayed format varies depending on the value to be converted.
If precision is not specified, a 6-digit value is displayed. If precision is specified, a value containing the specified number of digits is displayed. The 0 at the end of the conversion result is deleted.
c
Displays the first byte of the form argument.
s
Interprets and displays a form argument as a character string.
If the precision is not specified, the entire character string is displayed. If the precision is specified, a value of the specified number of bytes is displayed.
However, note that when the precision is specified and the delimiter to be output is in the middle of a multibyte character string, depending on the specifications of the OS, the number of bytes that is output might be less than the specified value.
%
Displays the % sign.
This conversion specifier cannot be used to specify flag characters, minimum field width, or precision.
b
Interprets and displays a form argument as a character string.
Escape characters contained in a character string are also interpreted, converted, and displayed as character strings.
However, if \c appears in the character string of the form argument, conversions after \c are not displayed.
This conversion specifier cannot be used to specify flag characters, minimum field width, or precision.
-
Escape characters
The following table shows the escape characters that can be used:
Escape character
Meaning
\a
Alert character (bell)
\b
Backspace character
\f
Form field character (page break)
\n
Linefeed character
\r
Carriage return character
\t
Tab character
\v
Vertical tab character
\d, \dd, \ddd
ASCII code characters displayed in octal notation (0 to 7) with 1 to 3 digits
\xhex
ASCII characters displayed in hexadecimal notation (0 to 9, a to f, and A to F) with 1 or 2 digits
\\
Display of \
\'
Display of '
\"
Display of "
If \ is contained in a character string other than the above, \ is also output.
Form arguments
-
If the number of items to be converted that is specified for form arguments is greater than that specified for forms, the forms are used repeatedly. The extra forms are assumed to be 0 or NULL.
Example
$ printf "%x %d " 123 456 789 7b 456 315 0
-
In a conversion of a numeric value, if a single quotation mark (') or double quotation mark (") is added before the character in the form argument, the conversion result is output in ASCII code.
Example
$ printf "%x %x" \'a \"b 61 62
-
If a numeric value is specified for the form argument of one of the following conversion specifiers, an octal number (0 specification), decimal number, or hexadecimal number (0x specification) can be specified:d, i, o, u, x, and X.
Example
$ printf "%d %d %d" 010 10 0x10 8 10 16
Return codes
Return code |
Meaning |
---|---|
0 |
Normal termination. |
1 |
Termination with an error. |
Notes
In Windows, linefeed characters are output as [CR]+[LF]. In UNIX, linefeed characters are output as [LF].
When the printf command is used to output and convert a value by specifying any of the following conversion specifiers, the value to be converted is handled as a 4-byte integer: d, i, o, u, x, and X. When a value is output or converted by specifying any of the following conversion specifiers, the value to be converted is handled as a double-precision floating point number (8 bytes): e, E, f, g, and G. For this reason, the conversion result has a margin of error. This error is dependent on the OS.
Examples
Output the hexadecimal value after conversion by specifying #0 as the flag character and 10 as the minimum field width.
$ ./printf "%#010x\n" 123 0x0000007b
Output the decimal value after conversion by specifying #+ as the flag character.
$ ./printf "%#+d\n" 123 +123
Output the result of converting a floating point number to the f format by specifying 8 as the precision.
$ ./printf "%.8f\n" 123.456 123.45600000
Output the result of converting a specified floating point number to the e format.
$ ./printf "%e\n" 123.456 1.234560e+02
Output one byte of the character string abcdef.
$ ./printf "%c\n" abcdef a
-
Output the character string abcdef by specifying 3 as the precision.
$ ./printf "%.3s\n" abcdef abc
-
Output the value specified for the form argument by specifying an escape character (\t) for the form.
$ ./printf "%s %d\txyz\n" abc 123 abc 123 xyz
-
Output a value by specifying 0 as the flag character and * (asterisk) as the precision.
In this example, * (asterisk) is replaced by 5.
$ ./printf "%0*d" 5 123 00123