Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

printf(3S)

printf(1)

NAME

printf − print formatted output

SYNOPSIS

printf format [argument...]

AVAILABILITY

SUNWloc

DESCRIPTION

The printf command converts, formats, and prints its arguments under control of the format. It fully supports conversion specifications for strings (%s descriptor); however, the results are undefined for the other conversion specifications supported by printf(3S). 

OPTIONS

format A character string that contains three types of objects: 1) plain characters, which are simply copied to the output stream; 2) conversion specifications, each of which results in fetching zero or more arguments; and 3) C-language escape sequences, which are translated into the corresponding characters.

argument String(s) to be printed under the control of format. The results are undefined if there are insufficient arguments for the format. If the format is exhausted while arguments remain, the excess arguments are simply ignored.

Each conversion specification is introduced by the character %.  After the %, the following appear in sequence:

An optional field, consisting of a decimal digit string followed by a $, specifying the next argument to be converted.  If this field is not provided, the argument following the last argument converted is used. 

An optional decimal digit string specifying a minimum field width. If the converted value has fewer characters than the field width, it is padded on the left (or right, if the left-adjustment flag ‘−’ has been given) to the field width.  The padding is with blanks unless the field width digit string starts with a zero, in which case the padding is with zeros. 

An optional precision that gives the maximum number of characters to be printed from a string in %s conversion.  The precision takes the form of a period (.) followed by a decimal digit string; a null digit string is treated as zero (nothing is printed).  Padding specified by the precision overrides the padding specified by the field width.  That is, if precision is specified, its value is used to control the number of characters printed. 

A field width or precision or both may be indicated by an asterisk (∗) instead of a digit string.  In this case, an integer argument supplies the field width or precision.  The argument that is actually converted is not fetched until the conversion letter is seen, so the arguments specifying field width or precision must appear before the argument (if any) to be converted.  A negative field width argument is taken as a ‘−’ (left-adjustment) flag followed by a positive field width.  If the precision argument is negative, it is changed to zero (nothing is printed).  In no case does a non-existent or small field width cause truncation of a field; if the result of a conversion is wider than the field width, the field is simply expanded to contain the conversion result. 

The conversion characters and their meanings are:

%s The argument is taken to be a string and characters from the string are printed until a null character (\0) is encountered or the number of characters indicated by the precision specification is reached.  If the precision is missing, it is taken to be infinite, so all characters up to the first null character are printed.  A null value for argument yields undefined results. 

%% Print a %; no argument is converted. 

EXAMPLES

The command

example% printf ’%s %s %s\n’ Good Morning World

results in the output:

Good Morning World

The following command produces the same output. 

example% printf ’%2$s %s %1$s\n’ World Good Morning

Here is an example that prints the first 6 characters of $PATH left-adjusted in a 10-character field:

example% printf ’First 6 chars of %s are %-10.6s.\n’ $PATH $PATH

If $PATH has the value /usr/bin:/usr/local/bin, then the above command would print the following output:

First 6 chars of /usr/bin:/usr/local/bin are /usr/b .

SEE ALSO

printf(3S)

SunOS 5.2  —  Last change: 14 Sep 1992

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026