[ Previous | Next | Contents | Glossary | Home | Search ]
AIX Version 4.3 Commands Reference, Volume 4

printf Command

Purpose

Writes formatted output.

Syntax

printf FormatArgument ... ]

Description

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:

LANG Determines the locale to use for the locale categories when both LC_ALL and the corresponding environment variable (beginning with LC_) do not specify a locale.
LC_ALL Determines the locale to be used to override any values for locale categories specified by the setting of LANG or any other LC_ environment variable.
LC_CTYPE Determines the locale for the interpretation of sequences of bytes of text data as characters; for example, single versus multibyte characters in parameters.
LC_MESSAGES Determines the language in which messages should be written.
LC_NUMERIC Determines the locale for numeric formatting. This environment variable affects the format of numbers written using the e, E, f, g, and G conversion characters.

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:

  1. A % (percent sign).
  2. Zero or more options, which modify the meaning of the conversion specification. The option characters and meanings are:
    - 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 appear, 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 appear, 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.
    For other conversions, the behavior is undefined.
  3. An optional decimal digit string that specifies the minimum field width. If the converted value has fewer characters than the field width, the field is padded on the left to the length specified by the field width. If the left-adjustment option is specified, the field is padded on the right. If the result of a conversion is wider than the field width, the field is expanded to contain the converted result. No truncation occurs. However, a small precision may cause truncation on the right.
  4. An optional precision. The precision is a . (dot) followed by a decimal digit string. If no precision is given, it is treated as 0 (zero). The precision specifies:
  5. A character that indicates the type of conversion to be applied, such as:
    % 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 appear. 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 appear. 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 appear. 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 appear. 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 appears.
    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 appears. 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 appears 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:

Exit Status

This command returns the following exit values:

0 Successful completion.
>0 An error occurred.

Examples

  1. Enter the following command:
    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.
  2. Enter the following command:
    printf "%c %c" 78 79
    This produces the following output:
    7 7

Files

/usr/bin/printf Contains the printf command.

Related Information

The /usr/bin/echo command.

The printf subroutine in Technical Reference: Base Operating System and Extensions.

The Input and Output Handling Programmer's Overview in AIX 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 for System Management in AIX Version 4.3 System Management Guide: Operating System and Devices.


[ Previous | Next | Contents | Glossary | Home | Search ]