Contained WithinFind More DocumentationFeatured Support Resources | Download this book in PDF (4620 KB)
printf(1)NAME | SYNOPSIS | DESCRIPTION | OPERANDS | USAGE | EXAMPLES | ENVIRONMENT VARIABLES | EXIT STATUS | ATTRIBUTES | SEE ALSO NAME
SYNOPSIS
DESCRIPTIONThe printf command writes formatted operands to the standard output. The argument operands are formatted under control of the format operand. OPERANDS
The following operands are supported: USAGENote that this printf utility, like the printf(3C) function on which it is based, makes no special provision for dealing with multi-byte characters when using the %c conversion specification or when a precision is specified in a %b or %s conversion specification. Applications should be extremely cautious using either of these features when there are multi-byte characters in the character set. Field widths and precisions cannot be specified as *. For compatibility with previous versions of SunOS 5.x, the $ format specifier is supported for formats containing only %s specifiers. The %b conversion specification is not part of the ISO C standard; it has been added here as a portable way to process backslash escapes expanded in string operands as provided by the echo utility. See also the USAGE section of the echo(1) manual page for ways to use printf as a replacement for all of the traditional versions of the echo utility. If an argument cannot be parsed correctly for the corresponding conversion specification, the printf utility reports an error. Thus, overflow and extraneous characters at the end of an argument being used for a numeric conversion are to be reported as errors. It is not considered an error if an argument operand is not completely used for a c or s conversion or if a string operand's first or second character is used to get the numeric value of a character. EXAMPLESExample 1 Printing a series of promptsTo alert the user and then print and read a series of prompts:
Example 2 Printing a table of calculationsTo read out a list of right and wrong answers from a file, calculate the percentage correctly, and print them out. The numbers are right-justified and separated by a single tab character. The percentage is written to one decimal place of accuracy:
Example 3 Printing number stringsThe command:
produces:
Note that the format operand is used three times to print all of the given strings and that a 0 was supplied by printf to satisfy the last %4d conversion specification. Example 4 Tabulating conversion errorsThe printf utility tells the user when conversion errors are detected while producing numeric output; thus, the following results would be expected on an implementation with 32-bit twos-complement integers when %d is specified as the format operand:
Note that the value shown on standard output is what would be expected as the return value from the function strtol(3C). A similar correspondence exists between %u and strtoul(3C), and %e, %f and %g and strtod(3C). Example 5 Printing output for a specific localeIn a locale using the ISO/IEC 646:1991 standard as the underlying codeset, the command:
produces:
Note that in a locale with multi-byte characters, the value of a character is intended to be the value of the equivalent of the wchar_t representation of the character. If an argument operand cannot be completely converted into an internal value appropriate to the corresponding conversion specification, a diagnostic message is written to standard error and the utility does exit with a zero exit status, but continues processing any remaining operands and writes the value accumulated at the time the error was detected to standard output. ENVIRONMENT VARIABLESSee environ(5) for descriptions of the following environment variables that affect the execution of printf: LC_COLLATE, LC_CTYPE, LC_MESSAGES, LC_TIME, TZ, and NLSPATH. EXIT STATUSATTRIBUTESSee attributes(5) for descriptions of the following attributes:
SEE ALSOawk(1), bc(1), echo(1), printf(3C), strtod(3C), strtol(3C), strtoul(3C), attributes(5), environ(5), formats(5) NAME | SYNOPSIS | DESCRIPTION | OPERANDS | USAGE | EXAMPLES | ENVIRONMENT VARIABLES | EXIT STATUS | ATTRIBUTES | SEE ALSO |
||||||||||||||||||||||||||||||||||||||