NAME
printf, sprintf, snprintf, printerr- print formatted output
SYNOPSIS
$(NUCLEUS_DIR)/lib/embedded/libebd.s.a
#include <stdio.h>
int
printf(const char *
format, ... /* args */);
int
sprintf(char *
s, const char *
format, ... /* args */);
int
snprintf(char *
s, size_t
size, const char *
format, ... /* args */);
int
printerr(const char *
format, ... /* args */);
API RESTRICTIONS
The function or functions documented here may not be used safely in
all application contexts with all APIs provided in the ChorusOS 5.0
product.
See
API(5FEA)
for details.
DESCRIPTION
The
printf()
function sends output to the standard
output channel, which is system defined. The
printerr()
function sends output to the standard error channel, which is system-defined.
The
sprintf()
function sends output, followed by the null
character (
\\0
), in consecutive bytes starting at *
s
; it is the user's responsibility to ensure that enough storage
is available. Each function returns the number of characters transmitted (not
including the
\\0
in the case of
sprintf()
),
or a negative value if an output error was encountered.
The
snprintf()
function writes at most
size-1
of the characters printed to the output string (the
size
character then gets the terminating zero). If the return
value is greater than or equal to the
size
argument,
the string was too short and some of the printed characters have been discarded.
Each of these functions converts, formats, and prints its
arg
s under control of the
format
.
The format has the general form:
%[flags] [width] [.precision] [{h | l | ll | q | L }]conversion_code
This
format
is a character string that contains
two types of objects: plain characters, which are simply copied to the output
channel, and conversion specifications, each of which results in obtaining
zero or more
arg
s. The results are undefined if there
are insufficient
arg
s for the format. If the format
is exhausted while
arg
s remain, the excess
arg
s are simply ignored.
Flags
Width
The
width
field is an optional decimal digit
string specifying a minimum field width. If the converted value has fewer
characters than the field width, it will be padded on the left (or right,
if the left-adjustment flag `-', described above, has been set) to the field
width. If the field width for an
s
conversion is preceded
by a
0
, the string is right adjusted with zero-padding
on the left.
The
width
field may be indicated by an asterisk
(
*
) instead of a digit string. In this case, an integer
arg
supplies the field width or precision. The
arg
that is actually converted is not fetched until the conversion
letter is seen, so the
arg
s specifying field width
or precision must appear before the
arg
(if any) to
be converted.
Precision
The
precision
field is an optional field that
gives the minimum number of digits to appear for the
d
,
o
,
u
,
x
, or
X
conversions, the number of digits to appear after the decimal point for the
e
and
f
conversions, the maximum number of significant
digits for the
g
conversion, or the maximum number of characters
to be printed from a string in an
s
conversion. The precision
takes the form of a dot (
.
) followed by a decimal digit
string. A null digit string is treated as zero.
As for the
width
field, the
precision
field may be indicated by an asterisk (*). The meaning is the
same as in the width field above.
Argument Size
An optional
h
,
l
(ell),
q
, or
ll
(ell ell) specifies the expected size
of the actual argument for the
d
,
i
,
o
,
u
,
x
or
X
conversion codes. Ignored if specified before any other conversion code.
Their meanings are:
-
h
-
The argument is a short integer.
-
l
-
The argument
is a long integer.
-
q
-
The argument
is a quad integer.
-
ll
-
The argument
is a long long integer.
An optional
l
(ell), or
L
specifies
the expected size of the actual argument for the
e
,
f
, or
g
conversion codes.
They are ignored if specified before any other conversion code.
Their meanings are:
-
l
-
The argument is a double.
-
L
-
The argument
is a long double.
Conversion Codes
The conversion characters and their meanings are:
-
d
,
i
,
o
,
u
,
x
,
X
-
The integer
arg
is
converted to signed decimal (
d
or i
),
unsigned octal (
o
), decimal (
u
), or
hexadecimal notation (
x
and
X
), respectively.
The letters
abcdef
are used for
x
conversion
and the letters
ABCDEF
for
X
conversion. 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 zeroes. (For compatibility with older versions,
padding with leading zeroes may alternatively be specified by prepending a
zero to the field width. This does not imply an octal value for the field
width.) The default precision is 1. The result of converting a zero value
with a precision of zero is a null string.
-
D,O,U
-
These notations
are semantically equivalent to, respectively,
ld
,
lo
,
lu
.
-
n
-
The arg should
be a pointer to an integer into which is written the number of bytes
written to the standard output I/O stream so far by this call. No argument
is converted.
-
p
-
The arg should
be a pointer to void. The value of the pointer is converted to a set of
sequences of printable characters.
-
f
-
The float
or double
arg
is converted to decimal notation in the
style ``[
-
]ddd
.
ddd,'' where the
number of digits after the decimal point is equal to the precision specification.
If the precision is not specified, six digits are output; if the precision
is explicitly 0, no decimal point appears.
-
e
,
E
-
The float or double
arg
is converted in the style
``[
-
]d
.
ddd
e+-
dd,''
where there is one digit before the decimal point and the number of digits
after it is equal to the precision. If the precision is not specified, six
digits are produced; if the precision is explicitly 0, no decimal point appears.
The
E
format code will produce a number with
E
instead of
e
introducing the exponent. The
exponent always contains at least two digits.
-
g
,
G
-
The float or double
arg
is printed in style
f
or
e
(or in style
E
in the
case of a
G
format code), with the precision specifying
the number of significant digits. The style used depends on the value converted:
style
e
will be used only if the exponent resulting from
the conversion is less than -4 or greater than the precision. Trailing
zeroes are removed from the result; a decimal point appears only if it is
followed by a digit.
-
c
-
The character
arg
is printed.
-
s
-
The
arg
is taken to be a string (character pointer) and characters
from the string are printed until a null character (
\\0
)
is encountered, or until the number of characters indicated by the precision
specification is reached. If the precision is not specified, it is assumed
to be infinite and all characters up to the first null character are printed.
A
NULL
value for
arg
will yield
undefined results.
-
%
-
Print a
%
; no argument is converted.
A non-existent or small field width will never 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. Characters generated
by
printf()
are printed in the same way as if
putchar
(3STDC) had been called.
Each conversion specification is introduced by the character
%
. After the
%
, the following appear in sequence:
-
Zero or more
flags
, which modify the
meaning of the conversion specification.
-
An optional decimal digit string specifying a minimum
field width
. If the converted value has fewer characters than
the field width, it will be padded on the left (or right, if the left-adjustment
flag `
&ndash
;', described below, has been set)
to the field width. If the field width for an s conversion is preceded by
a 0, the string is right-adjusted with zero-padding on the left.
-
An optional
l
(ell) specifying that a following
d
,
o
,
u
,
x
,
or
X
conversion character applies to a long integer
arg
. An
l
before any other conversion character
is ignored.
-
A character that indicates the type of conversion to be applied.
EXAMPLES
To print a date and time in the form ``Sunday, July 3, 10:02,'' where
weekday
and
month
are pointers to null-terminated
strings:
printf("%s, %s %d, %d:%.2d", weekday, month, day, hour, min);
|
ATTRIBUTES
posted on 2007-12-07 01:10
踏雪赤兔 阅读(2950)
评论(1) 编辑 收藏 引用 所属分类:
速查手册 、
玩转编程