506 lines · 9.5 KB · Text
RawDownload
.TH FPRINTF 2.SH NAMEfprintf, printf, sprintf, snprintf, vfprintf, vprintf, vsprintf, vsnprintf \- print formatted output.SH SYNOPSIS.B #include <u.h>.br.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 snprintf(char *s, int n, char *format, ...).PP.Bint vfprintf(FILE *f, char *format, va_list args).PP.Bint vprintf(char *format, va_list args).PP.Bint vsprintf(char *s, char *format, va_list args).PP.Bint vsnprintf(char *s, int n, char *format, va_list 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..I Snprintfis like .I sprintfbut writes at most .I nbytes (including the null character)into .IR s ..IR Vfprintf ,.IR vprintf ,.IR vsnprintf ,and.I vsprintfare the same, except the.I argsargument is the 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 and friends),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),

Lines 1–500 of 506.