.TH FPRINTF 2.SH NAMEfprintf, printf, sprintf, vfprintf, vprintf, vsprintf \- print formatted output.SH SYNOPSIS.B #include <stdio.h>.PP.Bint fprintf(FILE *f, char *format, ...);.PP.Bint printf(char *format, ...);.PP.Bint sprintf(char *s, char *format, ...);.PP.Bint vfprintf(FILE *f, char *format, char *args);.PP.Bint vprintf(char *format, char *args);.PP.Bint vsprintf(char *s, char *format, char *args);.SH DESCRIPTION.I Fprintfplaces output on the named output stream.I f(see.IR fopen (2))..I Printfplaces output on the standard output stream.IR stdout ..I Sprintfplaces outputfollowed by the null character.RB ( \e0 )in consecutive bytes starting at.IR s ;it is the user's responsibility to ensure thatenough storage is available..IR Vfprintf ,.IR vprintf ,and.I vsprintfare the same, except the.I argsargument is a pointer to an argument in an argument list of thecalling function, and the effect is as if the calling function'sargument list from that point on is passed to the.I printfroutines..PPEach function returns the number of characterstransmitted (not including the.B \e0in the case of.IR sprintf ),ora negative value if an output error was encountered..PPThese functionsconvert, format, and print theirtrailing argumentsunder control of a.IR format string.The.I formatcontains two types of objects:plain characters, which are simply copied to theoutput stream,and conversion specifications,each of which results in fetching ofzero or morearguments.The results are undefined if there are arguments of thewrong type or too fewarguments for the format.If the format is exhausted whilearguments remain, the excessare ignored..PPEach conversion specification is introduced bythe character.BR % .After the.BR % ,the followingappear in sequence:.PP.RSZero or more.IR flags ,which modify the meaning ofthe conversion specification..PPAn optional decimal digit string specifying a minimum.IR "field width" .If the converted value has fewer charactersthan the field width, it will be padded with spaces on theleft (or right, if the left adjustment, described later, has been given)to the field width..PPAn optional.I precision\^that givesthe minimum number of digits to appear for the.BR d ,.BR i ,.BR o ,.BR u ,.BR x ,and.B Xconversions,the number of digits to appear after thedecimal point for the.BR e ,.BR E ,and.B fconversions,the maximum number of significant digitsfor the.B gand.B Gconversions,or the maximum number of charactersto be written from a string in.B sconversion.The precision takes the form of a period.RB ( \&. )followed by an optional decimal integer;if the integer is omitted, it is treated as zero..PPAn optional.B hspecifying that a following.BR d ,.BR i ,.BR o ,.BR u ,.BR xor.BR Xconversion specifier applies to a.B short.B intor.B unsigned.B shortargument (the argument will have been promoted according to the integralpromotions, and its value shall be converted to.B shortor.B unsigned.B shortbefore printing);an optional.B hspecifying that a following.B nconversion specifier applies to a pointer to a.B shortargument;an optional.B l(ell) specifying that a following.BR d ,.BR i ,.BR o ,.BR u ,.BR x ,or.B Xconversion character applies to a.B longor.B unsigned.B longargument;an optional.B lspecifying that a following.B nconversion specifier applies to a pointer to a.B long.B intargument;or an optional.B Lspecifying that a following.BR e ,.BR E ,.BR f ,.BR g ,or.B Gconversion specifier applies to a.B long doubleargument.If an.BR h ,.BR l ,or.B Lappears with any other conversion specifier, the behavior is undefined..PPA character that indicates the type ofconversion to be applied..RE.PPA field width or precision, or both, may beindicated by an asterisk.RB ( * )instead of a digit string.In this case, an.B int.I arg\^suppliesthe field width or precision.The arguments specifying field width or precision, or both,shall appear (in that order) before the argument (if any) to be converted.A negative field width argument is taken as a.B -flag followed by a positive field width.A negative precision is taken as if it were missing..PPThe flag characters and their meanings are:.PD 0.TP 10.B -The result of the conversion is left-justified within the field..TP.B +The result of a signedconversion always begins with a sign.RB ( +or.BR - )..TPblankIf the first character of a signed conversion is not a sign,or a signed conversion results in no characters,a blankis prefixed to the result.This implies that if the blank and.B +flags both appear, the blank flag is ignored..TP.B #The result is to be convertedto an ``alternate form.''For.B oconversion, it increases the precision to forcethe first digit of the result to be a zero.For.B xor.B Xconversion, a non-zero result has.B 0xor.B 0Xprefixed to it.For.BR e ,.BR E ,.BR f ,.BR g ,and.B Gconversions, the result always contains a decimal point,even if no digits follow the point (normally, a decimal pointappears in the result of these conversions only if a digitfollows it).For.B gand.B Gconversions, trailing zeros are.I not\^be removed from the resultas they normally are.For other conversions, the behavior is undefined..TP.B 0For.BR d ,.BR i ,.BR o ,.BR u ,.BR x ,.BR X ,.BR e ,.BR E ,.BR f ,.BR g ,and.B Gconversions, leading zeros (following any indication of sign or base)are used to pad the field width; no space padding is performed.If the.B 0and.B -flags both appear, the.B 0flag will be ignored.For.BR d ,.BR i ,.BR o ,.BR u ,.BR x ,and.B Xconversions, if a precision is specified, the.B 0flag will be ignored.For other conversions, the behavior is undefined..PD.PPThe conversion charactersand their meanings are:.PP.PD 0.TP 10\fLd\fP,\fLo\fP,\fLu\fP,\fLx\fP,\fLX\fPThe integer.I arg\^is converted to signed decimal.RB ( dor.BR i ),unsigned octal.RB ( o ),unsigned decimal.RB ( u ),or unsigned hexadecimal notation.RB ( xor.BR X );the letters.B abcdefare used for.B xconversion and the letters.B ABCDEFfor.B Xconversion.The precision specifies the minimum number of digitsto appear; if the value being converted can be representedin fewer digits, it is expanded with leading zeros.The default precision is 1.The result of converting a zero value with a precisionof zero is no characters..TP.BR fThe .B doubleargument is converted to decimal notationin the style[\-]\fIddd\fL.\fIddd\fR,where the number of digits after the decimal pointis equal to the precision specification.If the precisionis missing,it is taken as 6;if the precision is explicitly.LR 0 ,no decimal point appears..TP.BR e , EThe.B doubleargument is converted in the style[\-]\fId\fL.\fIddd\fLe\fR±\fIdd\fR,where there is one digit before the decimal point andthe number of digits after it is equal to theprecision;when the precision is missing, it is taken as 6;if the precision is zero, no decimal point appears.The.B Eformat code produces a number with.B Einstead of.B eintroducing the exponent.The exponent always contains at least two digits..TP.BR g , GThe.B doubleargument is printed in style.BR for.BR e(or in style.B Ein the case of a.B Gconversion specifier),with the precision specifying the number of significant digits.If an explicit precision is zero, it is taken as 1.The style used depends on the value converted:style.B eis used only if the exponent resulting fromthe conversion is less than \-4or greater than or equal to the precision.Trailing zeros are removed from the fractional portion of the result;a decimal point appears only if it is followed by a digit..TP.B cThe.B intargument is converted to an.B unsigned.BR char ,and the resulting character is written..TP.B sTheargument is taken to be a string (character pointer)and characters from the string are printed untila null character.RB ( \e0 )is encountered orthe number of characters indicated by the precisionspecification is reached.If the precision is missing, it is taken to be infinite, soall characters up to the first null character are printed.Azerovalue forthe argument yields undefined results..TP.B PThe.B void *argument is printed in an implementation defined way (for Plan 9:the address as hexadecimal number)..TP.B nThe argument shall be a pointer to an integer into which is.I writtenthe number of characters written to the output stream so far bythis call to.IR fprintf .No argument is converted..TP.B %Print a.BR % ;no argument is converted..PD.PPIf a conversion specification is invalid, the behavior is undefined..PPIf any argument is, or points to, a union or an aggregate(except for an array of character type using.B %sconversion, or a pointer cast to be a pointer to.B voidusing.B %Pconversion), the behavior is undefined..PPIn no case does a nonexistent or small field width cause truncationof a field; if the result of a conversion is wider than the field width,the field is expanded to contain the conversion result..SH SOURCE.B /sys/src/libstdio.SH SEE ALSO.IR fopen (2),.IR fscanf (2),.IR print (2).SH BUGSThere is no way to print a wide character (rune); use.IR print (2)or.IR bio (2).
.TH FPRINTF 2
.SH NAME
fprintf, printf, sprintf, vfprintf, vprintf, vsprintf \- print formatted output
.SH SYNOPSIS
.B #include <stdio.h>
.PP
.B
int fprintf(FILE *f, char *format, ...);
.PP
.B
int printf(char *format, ...);
.PP
.B
int sprintf(char *s, char *format, ...);
.PP
.B
int vfprintf(FILE *f, char *format, char *args);
.PP
.B
int vprintf(char *format, char *args);
.PP
.B
int vsprintf(char *s, char *format, char *args);
.SH DESCRIPTION
.I Fprintf
places output on the named output stream
.I f
(see
.IR fopen (2)).
.I Printf
places output on the standard output stream
.IR stdout .
.I Sprintf
places output
followed by the null character
.RB ( \e0 )
in consecutive bytes starting at
.IR s ;
it is the user's responsibility to ensure that
enough storage is available.
.IR Vfprintf ,
.IR vprintf ,
and
.I vsprintf
are the same, except the
.I args
argument is a pointer to an argument in an argument list of the
calling function, and the effect is as if the calling function's
argument list from that point on is passed to the
.I printf
routines.
.PP
Each function returns the number of characters
transmitted (not including the
.B \e0
in the case of
.IR sprintf ),
or
a negative value if an output error was encountered.
.PP
These functions
convert, format, and print their
trailing arguments
under control of a
.IR format
string.
The
.I format
contains two types of objects:
plain characters, which are simply copied to the
output stream,
and conversion specifications,
each of which results in fetching of
zero or more
arguments.
The results are undefined if there are arguments of the
wrong type or too few
arguments for the format.
If the format is exhausted while
arguments remain, the excess
are ignored.
.PP
Each conversion specification is introduced by
the character
.BR % .
After the
.BR % ,
the following
appear in sequence:
.PP
.RS
Zero or more
.IR flags ,
which modify the meaning of
the conversion specification.
.PP
An optional decimal digit string specifying a minimum
.IR "field width" .
If the converted value has fewer characters
than the field width, it will be padded with spaces on the
left (or right, if the left adjustment, described later, has been given)
to the field width.
.PP
An optional
.I precision\^
that gives
the minimum number of digits to appear for the
.BR d ,
.BR i ,
.BR o ,
.BR u ,
.BR x ,
and
.B X
conversions,
the number of digits to appear after the
decimal point for the
.BR e ,
.BR E ,
and
.B f
conversions,
the maximum number of significant digits
for the
.B g
and
.B G
conversions,
or the maximum number of characters
to be written from a string in
.B s
conversion.
The precision takes the form of a period
.RB ( \&. )
followed by an optional decimal integer;
if the integer is omitted, it is treated as zero.
.PP
An optional
.B h
specifying that a following
.BR d ,
.BR i ,
.BR o ,
.BR u ,
.BR x
or
.BR X
conversion specifier applies to a
.B short
.B int
or
.B unsigned
.B short
argument (the argument will have been promoted according to the integral
promotions, and its value shall be converted to
.B short
or
.B unsigned
.B short
before printing);
an optional
.B h
specifying that a following
.B n
conversion specifier applies to a pointer to a
.B short
argument;
an optional
.B l
(ell) specifying that a following
.BR d ,
.BR i ,
.BR o ,
.BR u ,
.BR x ,
or
.B X
conversion character applies to a
.B long
or
.B unsigned
.B long
argument;
an optional
.B l
specifying that a following
.B n
conversion specifier applies to a pointer to a
.B long
.B int
argument;
or an optional
.B L
specifying that a following
.BR e ,
.BR E ,
.BR f ,
.BR g ,
or
.B G
conversion specifier applies to a
.B long double
argument.
If an
.BR h ,
.BR l ,
or
.B L
appears with any other conversion specifier, the behavior is undefined.
.PP
A character that indicates the type of
conversion to be applied.
.RE
.PP
A field width or precision, or both, may be
indicated by an asterisk
.RB ( * )
instead of a digit string.
In this case, an
.B int
.I arg\^
supplies
the field width or precision.
The arguments specifying field width or precision, or both,
shall appear (in that order) before the argument (if any) to be converted.
A negative field width argument is taken as a
.B -
flag followed by a positive field width.
A negative precision is taken as if it were missing.
.PP
The flag characters and their meanings are:
.PD 0
.TP 10
.B -
The result of the conversion is left-justified within the field.
.TP
.B +
The result of a signed
conversion always begins with a sign
.RB ( +
or
.BR - ).
.TP
blank
If the first character of a signed conversion is not a sign,
or a signed conversion results in no characters,
a blank
is prefixed to the result.
This implies that if the blank and
.B +
flags both appear, the blank flag is ignored.
.TP
.B #
The result is to be converted
to an ``alternate form.''
For
.B o
conversion, it increases the precision to force
the first digit of the result to be a zero.
For
.B x
or
.B X
conversion, a non-zero result has
.B 0x
or
.B 0X
prefixed to it.
For
.BR e ,
.BR E ,
.BR f ,
.BR g ,
and
.B G
conversions, the result always contains a decimal point,
even if no digits follow the point (normally, a decimal point
appears in the result of these conversions only if a digit
follows it).
For
.B g
and
.B G
conversions, trailing zeros are
.I not\^
be removed from the result
as they normally are.
For other conversions, the behavior is undefined.
.TP
.B 0
For
.BR d ,
.BR i ,
.BR o ,
.BR u ,
.BR x ,
.BR X ,
.BR e ,
.BR E ,
.BR f ,
.BR g ,
and
.B G
conversions, leading zeros (following any indication of sign or base)
are used to pad the field width; no space padding is performed.
If the
.B 0
and
.B -
flags both appear, the
.B 0
flag will be ignored.
For
.BR d ,
.BR i ,
.BR o ,
.BR u ,
.BR x ,
and
.B X
conversions, if a precision is specified, the
.B 0
flag will be ignored.
For other conversions, the behavior is undefined.
.PD
.PP
The conversion characters
and their meanings are:
.PP
.PD 0
.TP 10
\fLd\fP,\fLo\fP,\fLu\fP,\fLx\fP,\fLX\fP
The integer
.I arg\^
is converted to signed decimal
.RB ( d
or
.BR i ),
unsigned octal
.RB ( o ),
unsigned decimal
.RB ( u ),
or unsigned hexadecimal notation
.RB ( x
or
.BR X );
the letters
.B abcdef
are used for
.B x
conversion and the letters
.B ABCDEF
for
.B X
conversion.
The precision specifies the minimum number of digits
to appear; if the value being converted can be represented
in fewer digits, it is 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
.BR f
The
.B double
argument is converted to decimal notation
in the style
[\-]\fIddd\fL.\fIddd\fR,
where the number of digits after the decimal point
is equal to the precision specification.
If the precision
is missing,
it is taken as 6;
if the precision is explicitly
.LR 0 ,
no decimal point appears.
.TP
.BR e , E
The
.B double
argument is converted in the style
[\-]\fId\fL.\fIddd\fLe\fR±\fIdd\fR,
where there is one digit before the decimal point and
the number of digits after it is equal to the
precision;
when the precision is missing, it is taken as 6;
if the precision is zero, no decimal point appears.
The
.B E
format code produces a number with
.B E
instead of
.B e
introducing the exponent.
The exponent always contains at least two digits.
.TP
.BR g , G
The
.B double
argument is printed in style
.BR f
or
.BR e
(or in style
.B E
in the case of a
.B G
conversion specifier),
with the precision specifying the number of significant digits.
If an explicit precision is zero, it is taken as 1.
The style used depends on the value converted:
style
.B e
is 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 portion of the result;
a decimal point appears only if it is followed by a digit.
.TP
.B c
The
.B int
argument is converted to an
.B unsigned
.BR char ,
and the resulting character is written.
.TP
.B s
The
argument is taken to be a string (character pointer)
and characters from the string are printed until
a null character
.RB ( \e0 )
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
zero
value for
the argument yields undefined results.
.TP
.B P
The
.B void *
argument is printed in an implementation defined way (for Plan 9:
the address as hexadecimal number).
.TP
.B n
The argument shall be a pointer to an integer into which is
.I written
the number of characters written to the output stream so far by
this call to
.IR fprintf .
No argument is converted.
.TP
.B %
Print a
.BR % ;
no argument is converted.
.PD
.PP
If a conversion specification is invalid, the behavior is undefined.
.PP
If any argument is, or points to, a union or an aggregate
(except for an array of character type using
.B %s
conversion, or a pointer cast to be a pointer to
.B void
using
.B %P
conversion), the behavior is undefined.
.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.
.SH SOURCE
.B /sys/src/libstdio
.SH SEE ALSO
.IR fopen (2),
.IR fscanf (2),
.IR print (2)
.SH BUGS
There is no way to print a wide character (rune); use
.IR print (2)
or
.IR bio (2).