Writes formatted output.
printf Format [ Argument ... ]
The printf command converts, formats, and writes its Argument parameters to standard output. The Argument parameters are formatted under control of the Format parameter. The formatted output line cannot exceed LINE_MAX bytes in length.
The following environment variables affect the execution of the printf command:
The Format parameter is a character string that contains three types of objects:
The Argument parameter is a list of one or more strings to be written to standard output under the control of the Format parameter.
The Format parameter is reused as often as necessary to satisfy the Argument parameters. Any extra c or s conversion specifications are evaluated as if a null string Argument were supplied; other extra conversion specifications are evaluated as if a 0 Argument were supplied. Where the Format parameter contains no conversion specifications and Argument parameters are present, the results are unspecified.
Each conversion specification in the Format parameter has the following syntax in this order:
- | The result of the conversion is left-aligned within the field. |
+ | The result of a signed conversion always begins with a sign (+ or -). |
blank | If the first character of a signed conversion is not a sign, a blank is prefixed to the result. If both the blank and + option characters are displayed, then the blank option character is ignored. |
# | This option specifies that the value is to be converted to an alternate form. For c, d, i, u, and s conversions, the option has no effect. For o conversion, it increases the precision to force the first digit of the result to be a, 0 (zero). For x and X conversions, a nonzero result has 0x, or 0X prefixed to it, respectively. For e, E, f, g, and G conversions, the result always contains a radix character, even if no digits follow the radix character. For g and G conversions, trailing zeros are not removed from the result as they usually are. |
0 | For d, i, o, u, x, e, E, f, g, and G conversions, leading zeroes (following any indication of sign or base) are used to pad to the field width, no space padding is performed. If the 0 (zero) and the - (minus sign) options are displayed, the 0 (zero) option is ignored. For d, i, o, u, x, and X conversions, if a precision is specified, the 0 (zero) option is ignored. |
Note
For other conversions,
the behavior is undefined. |
% | Performs no conversion. Prints a % (percent sign). |
d, i | Accepts an integer value and converts it to signed decimal notation. The precision specifies the minimum number of digits to be displayed. If the value being converted can be represented in fewer digits, it is expanded with leading zeros. The default precision is 1. The result of converting a zero value with a precision of zero is a null string. Specifying a field width with a zero as a leading character causes the field width value to be padded with leading zeros. |
o | Accepts an integer value and converts it to signed octal notation. The precision specifies the minimum number of digits to be displayed. If the value being converted can be represented in fewer digits, it is expanded with leading zeros. The default precision is 1. The result of converting a zero value with a precision of zero is a null string. Specifying a field width with a zero as a leading character causes the field width value to be padded with leading zeros. An octal value for field width is not implied. |
u | Accepts an integer value and converts it to unsigned decimal notation. The precision specifies the minimum number of digits to be displayed. If the value being converted can be represented in fewer digits, it is expanded with leading zeros. The default precision is 1. The result of converting a zero value with a precision of zero is a null string. Specifying a field width with a zero as a leading character causes the field width value to be padded with leading zeros. |
x, X | Accepts an integer value and converts it to hexadecimal notation. The letters abcdef are used for the x conversion and the letters ABCDEF are used for the X conversion. The precision specifies the minimum number of digits to be displayed. If the value being converted can be represented in fewer digits, it is expanded with leading zeros. The default precision is 1. The result of converting a zero value with a precision of zero is a null string. Specifying a field width with a zero as a leading character causes the field width value to be padded with leading zeros. |
f | Accepts a float or double value and converts it to decimal notation in the format [-] ddd.ddd. The number of digits after the radix character (shown here as the decimal point) is equal to the precision specification. The LC_NUMERIC locale category determines the radix character to use tin this format. If no precision is specified, then six digits are output. If the precision is 0 (zero), then no radix character will be displayed. |
e, E | Accepts a float or double value and converts it to the exponential form [-] d.dde{+|-}dd. There is one digit before the radix character (shown here as the decimal point) and the number of digits after the radix character is equal to the precision specification. The LC_NUMERIC locale category determines the radix character to use tin this format. If no precision is specified, then six digits are output. If the precision is 0 (zero), then no radix character will be displayed. The E conversion character produces a number with E instead of e before the exponent. The exponent always contains at least two digits. However, if the value to be printed requires an exponent greater than two digits, additional exponent digits are printed as necessary. |
g, G | Accepts a float or double value and converts it in the style of the f or e conversion characters (or E in the case of the G conversion), with the precision specifying the number of significant digits. Trailing zeros are removed from the result. A radix character is displayed only if it is followed by a digit. The style used depends on the value converted. Style g results only if the exponent resulting from the conversion is less than -4, or if it is greater than or equal to the precision. |
c | Accepts a value as a string and prints the first character in the string. |
s | Accepts a value as a string and prints characters from the string until the end of the string is encountered or the number of characters indicated by the precision is reached. If no precision is specified, all characters up to the first null character are printed. |
b | Accepts a value as a string, that may contain backslash-escape sequences. Bytes from the converted string are printed until the end of the string or number of bytes indicated by the precision specification is reached. If the precision is omitted, all bytes until the first null character are printed. |
The following backslash-escape sequences are supported:
This command returns the following exit values:
0 | Successful completion. |
>0 | An error occurred. |
printf "%5d%4d\n" 1 21 321 4321 54321
This produces the following output:
1 21 3214321 54321 0
The Format parameter is used three times to print all of the given strings. The 0 (zero) is supplied by the printf command to satisfy the last %4d conversion specification.
printf "%c %c" 78 79
This produces the following output:
7 7
/usr/bin/printf | Contains the printf command. |
The /usr/bin/echo command.
The printf subroutine in AIX 5L Version 5.2 Technical Reference: Base Operating System and Extensions.
The Input and Output Handling Programmer's Overview in AIX 5L Version 5.2 General Programming Concepts: Writing and Debugging Programs describes the files, commands, and subroutines used for low-level, stream, terminal, and asynchronous I/O interfaces.
The National Language Support Overview in AIX 5L Version 5.2 National Language Support Guide and Reference.