📄 printf.3
字号:
.\" @(#)printf.3s 6.3 (Berkeley) 6/5/86.\".TH PRINTF 3 "June 5, 1986".AT 3.SH NAMEprintf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf \- formatted output conversion.SH SYNOPSIS.nf.ft B#include <sys/types.h>#include <stdio.h>#include <stdarg.h>int printf(const char *\fIformat\fP \fR[\fP, \fIarg\fP\fR] ...\fP);int fprintf(FILE *\fIstream\fP, const char *\fIformat\fP \fR[\fP, \fIarg\fP\fR] ...\fP);int sprintf(char *\fIs\fP, const char *\fIformat\fP \fR[\fP, \fIarg\fP\fR] ...\fP);int snprintf(char *\fIs\fP, size_t \fIn\fP, const char *\fIformat\fP \fR[\fP, \fIarg\fP\fR] ...\fP);int vprintf(const char *\fIformat\fP, va_list \fIargs\fP);int vfprintf(FILE *\fIstream\fP, const char *\fIformat\fP, va_list \fIargs\fP);int vsprintf(char *\fIs\fP, const char *\fIformat\fP, va_list \fIargs\fP);int vsnprintf(char *\fIs\fP, size_t \fIn\fP, const char *\fIformat\fP, va_list \fIargs\fP);.ft R.fi.SH DESCRIPTION.B Printfplaces output on the standard output stream.BR stdout ..B Fprintfplaces output on the named output.IR stream ..B Sprintfplaces `output' in the string.IR s ,followed by the character `\e0'..B Snprintf(Minix-vmd only)is like.B sprintfexcept that no more than.IR n \-1characters are written to.I sfollowed by a `\e0'..PPThe.B v*printffunctions can be used to make functions like the first four by using the.BR stdarg (3)method to process the argument..PPEach of these functions converts, formats, and prints its arguments afterthe first under control of the first argument.The first argument is a character string which contains two types of objects:plain characters, which are simply copied to the output stream,and conversion specifications, each of which causes conversion and printingof the next successive.IR arg ..PPEach conversion specification is introduced by the character.BR % .The remainder of the conversion specification includesin the following order.TP\(buZero or more of following flags:.RS.TP\(bua `#' characterspecifying that the value should be converted to an ``alternate form''.For .BR c ,.BR d ,.BR s ,and.BR uconversions, this option has no effect. For .B oconversions, the precision of the number is increased to force the firstcharacter of the output string to a zero. For .BR x ( X )conversion, a non-zero result has the string .BR 0x ( 0X )prepended to it. For .BR e ,.BR E ,.BR f ,.BR g ,and.BR Gconversions, the result will always contain a decimal point, even if nodigits follow the point (normally, a decimal point only appears in theresults of those conversions if a digit follows the decimal point). For.B gand.B Gconversions, trailing zeros are not removed from the result as theywould otherwise be..TP\(bua minus sign `\-' which specifies.I "left adjustment"of the converted value in the indicated field;.TP\(bua `+' character specifying that there should always bea sign placed before the number when using signed conversions..TP\(bua space specifying that a blank should be left before a positive numberduring a signed conversion. A `+' overrides a space if both are used..RE.TP\(buan optional digit string specifying a.I "field width;"if the converted value has fewer characters than the field widthit will be blank-padded on the left (or right,if the left-adjustment indicator has been given) to make up the field width;if the field width begins with a zero,zero-padding will be done instead of blank-padding;.TP\(buan optional period.RB ` . 'which serves to separate the field width from the next digit string;.TP\(buan optional digit string specifying a.I precisionwhich specifies the number of digits to appear after thedecimal point, for e- and f-conversion, or the maximum number of charactersto be printed from a string;.TP\(buthe character.B lspecifying that a following.BR d ,.BR o ,.BR x ,or.B ucorresponds to a long integer.IR arg ..TP\(bua character which indicates the type ofconversion to be applied..PPA field width or precision may be `*' instead of a digit string.In this case an integer.I argsuppliesthe field width or precision..PPThe conversion charactersand their meanings are.TP.B doxThe integer.I argis converted to decimal, octal, orhexadecimal notation respectively..TP.B XLike.BR x ,but use upper case instead of lower case..TP.B fThe float or double.I argis converted to decimal notationin the style `[\fB\-\fR]ddd.ddd'where the number of d's after the decimal pointis equal to the precision specificationfor the argument.If the precisionis missing,6 digits are given;if the precision is explicitly 0, no digits andno decimal point are printed..TP.B eThe float or double.I argis converted in the style`[\fB\-\fR]d\fB.\fRddd\fBe\fR\(+-dd'where there is one digit before the decimal point andthe number after is equal to theprecision specification for the argument;when the precision is missing,6 digits are produced..TP.B gThe float or double.I argis printed in style.BR d ,in style.BR f ,or instyle.BR e ,whichever gives full precision in minimum space..TP.B cThe character.I argis printed..TP.B s.I Argis taken to be a string (character pointer)and characters from the string are printed untila null character or untilthe number of characters indicated by the precisionspecification is reached;however if the precision is 0 or missingall characters up to a null are printed..TP.B uThe unsigned integer.I argis converted to decimaland printed..TP.B %Print a `%'; no argument is converted..PPIn no case does a non-existent or small field widthcause truncation of a field;padding takes place only if the specified fieldwidth exceeds the actual width.Characters generated by.B printfare printed by .BR putc (3)..PP.B Examples.brTo print a date and time in the form `Sunday, July 3, 10:02',where.I weekdayand.I monthare pointers to null-terminated strings:.PP.RSprintf("%s, %s %d, %02d:%02d", weekday, month, day, hour, min);.RE.PPTo print.if n pi.if t \(*pto 5 decimals:.IPprintf("pi = %.5f", 4*atan(1.0));.SH "SEE ALSO".BR putc (3),.BR scanf (3),.BR ecvt (3),.BR stdarg (3).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -