printf - Windows command-line utility to format and print data
to the standard output
SYNOPSIS
printf format[argument...]
DESCRIPTION
The printf(1w) utility writes formatted operands to the
standard output. The argument operands are formatted under
the control of the format string.
ARGUMENTS
The printf(1w) utility accepts the following
arguments:
format
A string describing the format used to write the remaining
operands. The format specifications are described in the "Format
specifications" section later in this topic.
argument
These strings are written to the standard output, under the
control of formats. These are treated as strings if the
corresponding conversion character is b, c, or
s. Otherwise, it is evaluated as a C constant. Allows
leading plus (+) or minus
sign (-).
If an argument operand cannot be completely converted
into an internal value appropriate to the corresponding conversion
specification, a diagnostic message is written to the standard
error. In this case, the utility does not exit with a zero exit
status, but continues processing any remaining operands. It writes
the value accumulated at the time the error was detected, to the
standard output.
FORMAT SPECIFICATIONS
The format is a character string that contains three
types of objects defined as follows:
Characters
Characters other than escape sequences are copied to the
output.
Escape sequences
Represents nongraphic characters. The valid escape sequences
and associated actions are listed as follows:
\\
(Backslash) Prints the backslash character.
\a
(Alert) Attempts to alert the user through audible or visible
notification.
\b
(Backspace) Moves the printing position to one column before
the current position, unless the current position is the start of a
line.
\f
(Form feed) Moves the printing position to the initial printing
position of the next logical page.
\n
(Newline) Moves the printing position to the start of the next
line.
\r
(Carriage return) Moves the printing position to the start of
the current line.
\t
(Tab) Moves the printing position to the next tab position on
the current line. If there are no more tab positions left on the
line, the behavior is undefined.
\v
(Vertical tab) Moves the printing position to the start of the
next vertical tab position. If there are no more vertical tab
positions left on the page, the behavior is undefined.
\c
The \c object is not written; it causes the
printf utility to ignore any remaining characters in the
string operand containing it, any remaining string operands,
and any additional characters in the format operand.
In addition to the escape sequences, if \ddd is
specified (where ddd is an octal number), it is written as a
character with the numeric value specified by the octal number.
The interpretation of a backslash followed by any other sequence
of characters is unspecified.
Conversion specifications
These specify the output format of each argument. Each
conversion specification is introduced by the percent sign
(%). After the percent sign, the following
appear in sequence.
Flags
Zero or more flags, in any order, modifying the meaning of the
conversion specification. The flag characters and their
meanings are:
-
The result of the conversion is left-justified within the
field.
+
The result of a signed conversion always begins with a sign
(+ or -).
Space
If the first character of a signed conversion is not a sign, a
space character is prefixed to the result. This means that if the
space character and + flags both appear, the space character
flag is ignored.
#
The value that needs conversion to an alternative form.
For c, d, i, u, and s
conversions, the behavior is undefined.
For o conversion, it increases the precision to force
the first digit of the result to be a zero.
For x or X conversion, a nonzero result is
prefixed with 0x or 0X, 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, X,
e, E, f, g, and G conversions,
leading zeros (following any indication of sign or base) are used
to pad to the field width; no space padding is performed. If the
0 and - flags both appear, the 0 flag is
ignored.
For d, i, o, u, x, and
X conversions, if a precision is specified, the 0
flag is ignored. For other conversions, the behavior is
undefined.
Field width
An optional string of decimal digits to specify a minimum field
width. For an output field, if the converted value has fewer bytes
than the field width, it is padded on the left. It is padded on the
right if the left-adjustment flag (-),
described as follows, has been given to the field width).
Precision
Gives the minimum number of digits to appear for the d,
o, i, u, x, or X conversions
(the field is padded with leading zeros), the number of digits to
appear after the radix character for the e and f
conversions, the maximum number of significant digits for the
g conversion; or the maximum number of characters to be
written 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.
Conversion characters
Conversion characters indicate the type of conversion to be
applied. Each conversion character results in fetching zero or more
arguments. The results are undefined if there are insufficient
arguments for the format. If the format is exhausted while
arguments remain, the excess arguments are ignored.
The conversion characters and their meanings are:
d, i, o, u, x,
X
The integer argument is written as signed decimal
(d or i), unsigned octal (o),
unsigned decimal (u), or unsigned hexadecimal
notation (x and (X). The d and
i specifiers convert to signed decimal in the style
[-]dddd.
The precision component of the argument specifies the
minimum number of digits to appear. If the value being converted
can be represented in fewer digits than the specified minimum, it
is expanded with leading zeros. The default precision is 1. The
result of converting a zero value with a precision of 0 is no
characters.
If both the field width and precision are omitted, the
implementation can precede, follow, or precede and follow numeric
arguments of types d, i, and u with blank
characters; arguments of type o (octal) can be preceded with
leading zeros.
f
The floating point number argument is written in decimal
notation in the style [-]ddd.ddd,
where the number of digits after the radix character (shown here as
a decimal point) is equal to the precision specification. If
precision is omitted from the argument, six digits are
written after the radix character; if precision is
explicitly 0, no radix character appears.
e, E
The floating point number argument is written in the style
[-]d.ddde+-2dd, where
there is 1 digit before the radix character (shown here as a
decimal point) and the number of digits after it is equal to the
precision. When the precision is missing, six digits are written
after the radix character; if the + flag is 0, no radix
character appears. The E conversion character produces a
number with E instead of e introducing the exponent.
The exponent always contains at least two digits. If the value to
be written requires an exponent greater than two digits, however,
additional exponent digits are written as necessary.
g, G
The floating point number argument is written in style f
or e (or in style E in the case of a G
conversion character), with precision specifying the number
of significant digits. The style used depends on the value
converted: style g is used only if the exponent resulting
from the conversion is less than -4 or greater than or equal to
precision. Trailing zeros are removed from the result. A
radix character appears only if a digit follows it.
c
The character is written. If the string is specified as the
argument, the first character is written.
s
The argument is taken to be a string, and characters from the
string are written until the end of the string, or the number of
characters indicated by the precision specification of the argument
is reached. If the precision is omitted from the argument, it is
taken to be infinite. All characters up to the end of the string
are written.
b
The argument is taken to be a string that can contain
backslash-escape sequences. \0ddd, where
ddd is an octal number that is converted to a byte with the
numeric value specified by the octal number.
%
Writes a percent sign (%); no argument is
converted.
In no case does a nonexistent or insufficient 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 format operand is reused as often as necessary to
satisfy the argument operands. Any extra c or s
conversion specifications will be evaluated as if a null string
argument were supplied. Other extra conversion specifications are
evaluated as if a zero argument were supplied. If the format
operand contains no conversion specifications and argument
operands are present, the results are unspecified.
If a character sequence in the format operand begins with
a percent sign, but does not form a valid conversion specification,
the behavior is unspecified.