1
0
Files
irix-657m-src/eoe/man/man3s/printf.3s
2022-09-29 17:59:04 +03:00

513 lines
18 KiB
Plaintext

'\"macro stdmacro
.if n .pH g3s.printf @(#)printf 41.5 of 5/26/91
.\" Copyright 1991 UNIX System Laboratories, Inc.
.\" Copyright 1989, 1990 AT&T
'\" ident "@(#)svid_ba:ba_lib/printf 1.19"
.nr X
.if \nX=0 .ds x} printf 3S "" "\&"
.if \nX=1 .ds x} printf 3S ""
.if \nX=2 .ds x} printf 3S "" "\&"
.if \nX=3 .ds x} printf "" "" "\&"
.TH \*(x}
.SH NAME
\f4printf\f1, \f4fprintf\f1, \f4snprintf\f1, \f4sprintf\f1 \- print formatted output
.SH SYNOPSIS
.nf
.ft 4
#include <stdio.h>
.sp .5
int printf(const char \(**format, .../\(** args \(**/);
.sp .5
int fprintf(FILE \(**strm, const char \(**format, .../\(** args \(**/);
.sp .5
int snprintf(char \(**s, ssize_t len, const char \(**format, .../\(** args \(**/);
.sp .5
int sprintf(char \(**s, const char \(**format, .../\(** args \(**/);
.ft 1
.fi
.SH DESCRIPTION
.br
\f4printf\f1 places output on the standard output stream \f4stdout\f1.
.P
\f4fprintf\f1 places output on \f2strm\f1.
.P
\f4sprintf\f1 places output,
followed by a null character (\f4\e0\f1),
in consecutive bytes starting at \f2s\f1.
It is the user's responsibility to ensure
that enough storage is available.
.P
\f4snprintf\f1 places output,
followed by a null character (\f4\e0\f1),
in consecutive bytes starting at \f2s\f1.
If more than
\f4len\f1 bytes of output would be generated, the output is truncated at
\f4len\f1 bytes, including the trailing null character.
.P
Each function returns the number of characters transmitted
(not including the terminating null character in the case of \f4snprintf\fP
and \f4sprintf\f1)
or a negative value if an output error was encountered.
.P
Each of these functions converts, formats, and prints
its \f2args\f1 under control of the \f2format\f1.
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.
The \f2format\f1 is a character string that contains
two types of objects defined below:
.RS 5
.TP 4
1.
plain characters that are simply copied to the output stream;
.TP 4
2.
conversion specifications.
.RE
.P
All forms of the \f4printf\f1 functions allow for the insertion of a
language-dependent decimal-point character.
The decimal-point character is
defined by the program's locale (category
\f4LC_NUMERIC\f1).
In the \f4"C"\f1 locale, or in a locale where the
decimal-point character is not defined,
the decimal-point character defaults
to a period (\f4.\f1).
.P
Each conversion specification is introduced by the character \f4%\f1,
and takes the following general form and sequence:
.RS 5
.TP 4
\f4%\f1\&[\|\f2posp\f4$\f1\|]\&[\|\f2flags\f1\|]\&[\|\f2width\f1\|]\&[\|\f4.\f2precision\f1\|]\&[\|\f2size\f1\|]\&\f2fmt\f1
.RE
.TP 6
\f2posp\f4$\f1
An optional entry,
consisting of one or more decimal digits
followed by a \f4$\f1 character,
specifying the number of the
next \f2arg\f1 to access.
The first \f2arg\fP (just after \f2format\fP)
is numbered 1.
If this field is not specified,
the \f2arg\f1 following the most recently
used \f2arg\f1 will be used.
.TP 6
\f2flags\f1
Zero or more characters that
modify the meaning of the conversion specification.
The \f2flag\f1 characters and their meanings are:
.RS
.TP
\f4\'\f1
The integer portion of the result of a decimal conversion (for \f4b\fP,
\f4B\fP, \f4i\fP,
\f4d\fP, \f4u\fP, \f4f\fP, \f4g\fP, or \f4G\fP conversions) will be
formatted with the thousands' grouping characters.
The non-monetary grouping character will be used.
.TP
\f4\-\f1
The result of the conversion will be left-justified within the field.
(It will be right-justified if this flag is not specified.)
.TP
\f4\(pl\f1
The result of a signed conversion will always begin with a
sign (\f2\(pl\f1 or \f2\-\f1).
(It will begin with a sign only when a
negative value is converted if this flag
is not specified.)
.TP
\f2space\f1
If the first character of a signed conversion is not a sign,
or if a signed conversion results in no characters,
a space will be prefixed to the result.
If the \f2space\f1 and \f2\(pl\f1 flags both appear,
the \f2space\f1 flag will be ignored.
.TP
\f4#\f1
The value is to be converted to an alternate form.
For an \f4o\f1 conversion,
it increases the precision (if necessary) to force the
first digit of the result to be a zero.
For \f4x\f1 (or \f4X\f1) conversion,
a nonzero result will have \f40x\f1 (or \f40X\f1) prefixed to it.
For \f4b\f1, \f4B\f1,
\f4e\f1, \f4E\f1, \f4f\f1, \f4g\f1, and \f4G\f1 conversions,
the result will always contain a decimal-point character,
even if no digits follow it.
(Normally, a decimal point appears in the
result of these conversions only if a digit follows it.)
For \f4g\f1 and \f4G\f1 conversions,
trailing zeros will not be removed from
the result (as they normally are).
For \f4c\f1, \f4d\f1, \f4i\f1, \f4s\f1, and \f4u\f1 conversions,
the flag has no effect.
.TP
\f40\f1
For \f4b\f1, \f4B\f1,
\f4d\f1, \f4i\f1, \f4o\f1, \f4u\f1, \f4x\f1\^, \f4X\f1\^,
\f4e\f1, \f4E\f1, \f4f\f1, \f4g\f1, and \f4G\f1
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 \f40\f1 and \f4\(en\f1 flags both appear, the \f40\f1 flag
will be ignored.
For \f4d\f1, \f4i\f1, \f4o\f1, \f4u\f1, \f4x\f1\^, and \f4X\f1 conversions,
if a precision is specified,
the \f40\f1 flag will be ignored.
For other conversions, the behavior is undefined.
.RE
.TP 6
\f2width\fP
An optional entry that consists of either
one or more decimal digits,
or an asterisk (\f4\(**\fP),
or an asterisk followed by one or more decimal digits and a \f4$\fP.
It specifies the minimum field width:
If the converted value has fewer characters than the field width,
it will be padded
(with space by default)
on the left or right
(see the above \f2flags\fP description)
to the field width.
.TP 6
\f4\\.\f1\f2prec\f1
An optional entry that consists of a period (\f4.\fP) followed by
either zero or more decimal digits,
or an asterisk (\f4\(**\fP),
or an asterisk followed by one or more decimal digits and a \f4$\fP.
It specifies the minimum number of digits to appear
for the \f4d\f1, \f4i\f1, \f4o\f1, \f4u\f1, \f4x\f1\^, and \f4X\f1 conversions,
the number of digits to appear after the decimal-point character
for the \f4b\f1, \f4B\f1, \f4e\f1, \f4E\f1, and \f4f\f1 conversions,
the maximum number of significant digits
for the \f4g\f1 and \f4G\f1 conversions,
or the maximum number of characters to be written from a string
for an \f4s\f1 conversion.
For other conversions,
the behavior is undefined.
If only a period is specified,
the precision is taken as zero.
.TP 6
\f2size\fP
An optional \f4h\fP, \f4l\fP (ell), \f4ll\fP (ell ell), or \f4L\fP
that specifies other than the default argument type of
\f4int\fP for \f4d\fP and \f4i\fP;
\f4unsigned\fP \f4int\fP for \f4o\fP, \f4u\fP, \f4x\fP, and \f4X\fP;
pointer to \f4int\fP for \f4n\fP;
and \f4double\fP for \f4b\fP, \f4B\fP, \f4e\fP, \f4E\fP, \f4f\fP, \f4g\fP, and \f4G\fP.
If a \f2size\fP appears other than in the following combinations,
the behavior is undefined.
.RS
.TP
\f4h\fP
For \f4n\fP,
the argument has type pointer to \f4short\fP \f4int\fP;
for \f4d\fP and \f4i\fP,
\f4short\fP \f4int\fP;
and for \f4o\fP, \f4u\fP, \f4x\fP, and \f4X\fP,
\f4unsigned\fP \f4short\fP \f4int\fP.
(For \f4d\fP, \f4i\fP, \f4o\fP, \f4u\fP, \f4x\fP, and \f4X\fP,
the argument will have been promoted according to the integral promotions,
and its value will be narrowed to \f4short\fP or \f4unsigned\fP \f4short\fP
before printing.)
.TP
\f4l\fP
For \f4n\fP,
the argument has type pointer to \f4long\fP \f4int\fP;
for \f4d\fP and \f4i\fP,
\f4long\fP \f4int\fP;
and for \f4o\fP, \f4u\fP, \f4x\fP, and \f4X\fP,
\f4unsigned\fP \f4long\fP \f4int\fP.
.TP
\f4ll\fP
For \f4n\fP,
the argument has type pointer to \f4long\fP \f4long\fP \f4int\fP;
for \f4d\fP and \f4i\fP,
\f4long\fP \f4long\fP \f4int\fP;
and for \f4o\fP, \f4u\fP, \f4x\fP, and \f4X\fP,
\f4unsigned\fP \f4long\fP \f4long\fP \f4int\fP.
.TP
\f4L\fP
For \f4b\fP, \f4B\fP, \f4e\fP, \f4E\fP, \f4f\fP, \f4g\fP, and \f4G\fP,
the argument has type \f4long\fP \f4double\fP.
.RE
.TP 6
\f2fmt\fP
A conversion character (described below) that
indicates the type of conversion to be applied.
.P
When a \f2width\f1 or \f4.\f2prec\f1 includes an asterisk (\f4\(**\f1),
an \f4int\fP \f2arg\fP supplies the width or precision.
When they do not include a \f4$\fP,
the arguments specifying a field width, or precision, or both
must appear (in that order) before the argument (if any) to be converted.
If the conversion specification includes \f2posp\f4$\f1,
the field width and precision may include a \f4$\fP.
The decimal digits that precede the \f4$\fP
similarly specify the number of the \f2arg\fP
that contains the field width or precision.
(In this case,
\f2posp\f4$\f1 specifies the number of the \f2arg\fP to convert.)
A negative field width argument is taken
as a \f4\-\f1 flag followed
by a positive field width.
If the precision argument is negative,
it will be taken as zero.
.P
When numbered argument specifications are used,
specifying the \f2N\f1th argument requires that all the leading arguments,
from the first to the (\f2N\f1\-1)th,
be specified at least once,
in a consistent manner,
in the format string.
.P
The conversion characters and their meanings are:
.TP 10
\f4b\f1, \f4B\f1
The floating \f2arg\f1 is first converted to human readable byte
counts. \f2Arg\f1 is repeatedly divided by 1024 (\f4%b\f1) or 1000
(\f4%B\f1) until the value is less than 1000. The value is then
printed as with the \f4%f\f1 format except that there is a one
character postfix of '\ ', \f4K\f1, \f4M\f1, \f4G\f1, \f4T\f1, \f4P\f1,
\f4E\f1, \f4Z\fP, or \f4Y\fP.
The postfix is lower case for \f4%b\f1 and upper case for \f4%B\f1.
The postfix tag corresponds to bytes, kilobytes, megabytes, gigabytes, etc.
The definition of kilobyte is 2^10 for \f4%b\f1 and 10^3 for \f4%B\f1,
the definition of megabyte is 2^20 for \f4%b\f1 and 10^6 for \f4%B\f1,
and so on.
The full table is:
.if t .sp .5
.if n .sp
.nf
.ft CB
%b Multiplier From %B Multiplier
.if t ---------------------------------------------
.if n ---------------------------------------------
\'\ \' 1 \'\ \' 1
k 2^10 (1024) kilo K 10^3 (1000)
m 2^20 mega M 10^6
g 2^30 giga G 10^9
t 2^40 tera T 10^12
p 2^50 peta P 10^15
e 2^60 exa E 10^18
z 2^70 zetta Z 10^21
y 2^80 yotta Y 10^24
.if t .sp .5
.if n .sp
.fi
.ft 1
The default precision is 3, i.e., 1024 printed with \f4%b\f1 is printed
as if it were \f4%.3fk\f1 and will yield \f41.000k\f1.
.TP
\f4d\f1, \f4i\f1
The integer \f2arg\f1 is converted to signed decimal.
The precision specifies the minimum number of digits to appear;
if the value being converted can be represented in fewer digits,
it will be expanded with leading zeros.
The default precision is 1.
The result of converting a zero value with a
precision of zero is no characters.
.TP
\f4o\f1, \f4u\f1, \f4x\f1, \f4X\f1
The unsigned integer \f2arg\f1 is converted to
unsigned octal (\f4o\f1),
unsigned decimal (\f4u\f1),
or unsigned hexadecimal notation (\f4x\f1 and \f4X\f1).
The \f4x\f1 conversion uses the letters \f4abcdef\f1 and
the \f4X\f1 conversion uses the letters \f4ABCDEF\f1.
The precision specifies the minimum number of digits to appear;
if the value being converted can be represented in fewer digits,
it will be expanded with leading zeros.
The default precision is 1.
The result of converting a zero value with a
precision of zero is no characters.
.TP
\f4f\f1
The floating \f2arg\f1 is converted to decimal notation
in the style \f4[\-]\f2ddd\f4.\f2ddd\f1,
where the number of digits after the decimal-point character
[see \f4setlocale\fP(3C)] is equal to the precision specification.
If the precision is missing,
it is taken as 6;
if the precision is zero and the \f4#\f1 flag is not specified,
no decimal-point character appears.
If a decimal-point character appears,
at least one digit appears before it.
The value is rounded to the appropriate number of digits.
.TP
\f4e\f1, \f4E\f1
The floating \f2arg\f1 is converted to the style
.if t \f4[\-]\f2d\f4.\f2ddd\f4e\f2\(+-dd\f1,
.if n \f4[\-]\f2d\f4.\f2ddd\f4e\fP\*(eM\(+-\*(Me\fP\f2dd\f1,
where there is one digit before the decimal-point character
(which is nonzero if the argument is nonzero)
and the number of digits after it is equal to the precision.
If the precision is missing,
it is taken as 6;
if the precision is zero and the \f4#\f1 flag is not specified,
no decimal-point character appears.
The value is rounded to the appropriate number of digits.
The \f4E\f1 conversion character will produce a number
with \f4E\f1 instead of \f4e\f1 introducing the exponent.
The exponent always contains at least two digits.
If the value is zero,
the exponent is zero.
.TP
\f4g\f1, \f4G\f1
The floating \f2arg\f1 is converted in style \f4f\f1 or \f4e\f1
(or in style \f4E\f1 in the case of a \f4G\f1 conversion character),
with the precision specifying the number of significant digits.
If the precision is zero, it is taken as one.
The style used depends on the value converted;
style \f4e\f1 (or \f4E\f1) will be used only
if the exponent resulting from the conversion is less than \-4 or
greater than or equal to the precision.
Trailing zeros are removed from the fractional part of the result;
a decimal-point character appears only if it is followed by a digit.
.TP
\f4c\f1
The integer \f2arg\f1 is converted to an \f4unsigned char\f1,
and the resulting character is written.
.TP
\f4C\f1
\f2arg\f1 is interpreted as a \f4wchar_t\f1, converted to a multi-byte
sequence, and the resulting byte(s) are written.
.TP
\f4s\f1
The \f2arg\f1 is taken to be a pointer to an array of characters.
Characters from the array are written up to (but not including)
a terminating null character;
if a precision is specified,
no more than that many characters are written.
If a precision is not specified
or is greater than the size of the array,
the array must contain a terminating null character.
(A null pointer for \f2arg\f1 will yield undefined results.)
.TP
\f4S\f1
The \f2arg\f1 is taken to be a pointer to an array of wide characters
(\f4wchar_t\f1).
Each character from the array is converted to a multi-byte
sequence and the resulting byte(s) are written.
Conversion stops when there is a null wide character in the array.
If a precision is specified,
no more than that many characters are written.
If a precision is not specified
or is greater than the size of the array,
the array must contain a terminating null character.
(A null pointer for \f2arg\f1 will yield undefined results.)
.TP
\f4p\f1
The \f2arg\f1 is taken to be a pointer to \f4void\f1.
The value of the pointer is converted to an
implementation-defined sequence of printable characters,
which matches those read by
the \f4%p\f1 conversion of the \f4scanf\f1 function.
.TP
\f4n\f1
The \f2arg\f1 is taken to be a pointer to an integer
into which is written the
number of characters written so far by this
call to \f4printf\f1, \f4fprintf\f1, \f4snprintf\f1, or \f4sprintf\f1.
No argument is converted.
.TP
\f4%\f1
Print a \f4%\f1; no argument is converted.
The complete specification must be simply \f4%%\fP.
.P
If the form of the conversion specification
does not match any of the above,
the results of the conversion are undefined.
Similarly,
the results are undefined if there are insufficient \f2args\f1 for the format.
If the format is exhausted while \f2args\f1 remain,
the excess \f2args\f1 are ignored.
.PP
If a floating-point value is the internal representation for infinity,
the output is
.if t [\f4\(+-\f1]\f2inf\f1,
.if n [\fP\*(eM\(+-\*(Me\f1]\f2inf\f1,
where \f2inf\f1 is either
\f4inf\f1 or \f4INF\f1, depending on whether the conversion character
is lowercase or uppercase.
Printing of the sign follows the rules described above.
.PP
If a floating-point value is the internal representation for
``not-a-number,'' the output is
.if t [\f4\(+-\f1]\f2nan\f40\f2xm\f1.
.if n [\fP\*(eM\(+-\*(Me\f1]\f2nan\f40\f2xm\f1.
Depending on the conversion character, \f2nan\f1 is either
\f4nan\f1 or \f4NAN\f1.
Additionally, \f40\f2xm\f1 represents the most
significant part of the mantissa.
Again depending on the conversion character, \f2x\f1
will be \f4x\f1 or \f4X\f1, and \f2m\f1 will use the letters
\f4abcdef\f1 or \f4ABCDEF\f1.
Printing of the sign follows the rules described above.
.PP
In no case does a nonexistent or small field width
cause truncation of a field;
if the result of a conversion is wider than the field width,
the field is expanded to contain the conversion result.
Characters generated by \f4printf\f1 and \f4fprintf\f1
are printed as if the \f4putc\f1
routine had been called repeatedly.
.SH EXAMPLES
To print a date and time in the form ``Sunday, July 3, 10:02,''
where \f4weekday\f1 and \f4month\f1 are pointers to null-terminated strings:
.P
.RS
.nf
.ft 4
printf("%s,\ %s\ %i,\ %d:%.2d",\
\ \ \ \ \ \ \ \ weekday,\ month,\ day,\ hour,\ min);
.ft 1
.fi
.RE
.P
To print
.ie t \(*p
.el pi
to 5 decimal places:
.P
.RS
.nf
.ft 4
printf("pi\ \(eq\ %.5f",\ 4\ *\ atan(1.0));
.ft 1
.fi
.RE
.P
The following two calls to \f4printf\fP
both produce the same result of \f410\ 10\ 00300\ 10\fP:
.P
.RS
.nf
.ft 4
printf("%d %1$d %.*d %1$d", 10, 5, 300);
printf("%d %1$d %3$.*2$d %1$d", 10, 5, 300);
.ft 1
.fi
.RE
.SH "SEE ALSO"
\f4exit\fP(2),
\f4lseek\fP(2),
\f4write\fP(2),
\f4abort\fP(3C),
\f4ecvt\fP(3C),
\f4setlocale\fP(3C),
\f4putc\fP(3S),
\f4scanf\fP(3S),
\f4stdio\fP(3S).
.SH "DIAGNOSTICS"
.br
\f4printf\f1, \f4fprintf\f1, \f4snprintf\f1, and \f4sprintf\f1
return the number of characters transmitted
(not counting the terminating null character for \f4snprintf\fP and
\f4sprintf\fP),
or return a negative value if an error was encountered.
.br