📄 printf.3s
字号:
." SCCSID: @(#)printf.3s 2.4 8/12/87.\" Last modified by DAH on 11-Sept-88 1900..\".TH printf 3s.SH Nameprintf, fprintf, sprintf \- formatted output conversion.SH Syntax.B #include <stdio.h>.PP.B int printf(.I format[, .I arg] ....B ).br.B char .IR *format ;.PP.B int fprintf(.IR stream , .I format [, .I arg] ....B).br.B FILE .IR *stream ;.br.B char .IR *format ;.SS BSD Environment.B char *sprintf(.IR s , .I format [, .I arg] ....B ).br.B char .IR *s ,.IR *format ;.SS System V and POSIX Environments.B int sprintf(.IR s , .I format[,.I arg] ....B ).br.B char .IR *s ,.IR format ;.SH Description.NXR "printf function".NXR "fprintf function".NXR "sprintf function".NXR "formatted output" "printing"The.PN printffunction places output on the standard output stream,.PN stdout .The.PN fprintfsubroutine places output on the named output.IR stream .The.PN sprintfsubroutine places output in the string.IR s ,and appends the null terminator `\\0' to the end of the string..PPThe first argument controls how each of these functions converts,formats, and prints the other arguments. The first argument is a character stringthat contains two types of objects, characters and conversion specifications. These functions copy characters that appear in the first argument to theoutput stream. Conversion specifications cause these functions toconvert the next successive argument and send the formatted argument tothe output stream..PPYou introduce conversion specifications using the percent sign .BR (%).Following the.BR %,you can include:.NXR "printf subroutine" "conversion specification list".IP \(bu 5Zero or more flags, which modify the meaning of the conversion specification. .IP \(bu 5An optional minus sign (\-), which specifies left adjustment of the converted value in the indicated field..IP \(bu 5An optional digit string that specifies a field width.If the converted value has fewer characters than the field width,.PN printfpads the value with blanks. By default, .PN printfpads the value on the left. If the conversion string specifies the valueis left-justified, .PN printfpads the value on the right.If the field width begins with a zero,.PN printfpads the values with zeros, instead of blanks..IP \(bu 5An optional period (.), which separates the field width from the nextdigit string..IP \(bu 5An optional digit string specifying a precision. The precisioncontrols the number of digits that appear after the radix character,exponential and floating-point conversions. Precision also controlsthe maximum number of characters that are placed in the convertedvalue for a string..IP \(bu 5The character.B hor.B lspecifying that a following.BR d ,.BR i ,.BR o ,.BR u ,.BR x ,or.BR Xcorresponds to an integer or longword integer argument.You can use an uppercase .BR Lor a lowercase.BR l..IP \(bu 5A character that indicates the type ofconversion to be applied..PPA field width or precision can be an asterisk (*), instead of a digitstring.If you use an asterisk, you can include an argument that supplies thefield width or precision..PPThe flag characters and their meanings are as follows:.IP \fB\-\fP 5The result of the conversion is left-justifiedwithin the field..IP \fB+\fP 5The result of a signed conversion always begins with a sign (+ or \-)..IP \fBblank\fP 5If the first character of a signed conversion is nota sign, .PN printf pads the value on the left with a blank. If the blank and plus sign(+) flags both appear, .PN printf ignores the blank flag..IP \fB\#\fP 5The result has been converted to a different format.The value is to be converted to an alternative form. .IPFor.B c, d, s,and .B uconversions, this flag has no effect. .IPFor .B o conversions, this flag increases the precision to force the first digit of theresult to be a zero. .IPFor.B xor .B Xconversions, .PN printfpads a non-zero result on the left with .B 0xor .BR 0X ..IPFor .B e, E, f, g,and.B Gconversions, the result always contains a radix character,even if no digits follow that character. (A radix character usuallyappears in the result of these conversions only if a digit follows it.) .IPFor .B gand .B Gconversions, .PN printfdoes not remove trailing zeros from the result..PPThe conversion characters and their meanings are as follows:.IP \fBdox\fP 5Convert the integer argumentto decimal, octal, orhexadecimal notation, respectively..IP \fBf\fP 5Convert the floating point or double precision argument to decimalnotation in the style.IR [ \-.IR ]ddd.ddd , where the number of .IR d s following the radix character is equal to the precision for the argument.If the precision is missing, .PN printfprints six digits. If the precision is explicitly zero, the functionprints no digits andno radix characters..IP \fBe\fP 5Convert the floating point or double precision argument in the style .IR [ \-.IR ]d ..IR ddde\(+-dd , where one digit appears before the radix character and the number of digits that appear after the radix character is equal to the precision.When you omit the precision,.PN printfprints six digits..IP \fBg\fP 5Convert the floating point or double precision argument to style.BR d ,style.BR f ,or style.BR e .The style.PN printfuses depends on the format of the converted value. The functionremoves trailing zeros before evaluating the format of the convertedvalue..IPIf a radix character appears inthe converted value that is followed by a digit, .PN printf uses style .BR d .If the converted value contains an exponent that is is less than \-4 or greater than the precision, the function uses style \.BR e .Otherwise, the.PN printffunction uses style .BR f ..IP \fBc\fP 5Print the character argument..IP \fBs\fP 5Print the character argument. The.PN printffunction prints the argument until it encounters a null characters orhas printed the number of characters specified by the precision. Ifthe precision is zero or has not been specified, .PN printfprints the character argument until it encounters a null character..IP \fBu\fP 5Convert the unsigned integer argument to a decimal value. The resultmust be in the range of 0 through 4294967295, where the upper bound isdefined by MAXUNIT..IP \fBi\fP 5Convert the integer argument to decimal. (This conversion character isthe same as \fBd\fP.).IP \fBn\fP 5Store the number of characters formatted in the integer argument..IP \fBp\fP 5Print the pointer to the argument. (This conversion character is thesame as %08X)..IP \fB%\fP 5Print a percent sign ( % ). The function converts no argument..PPA non-existent or small field width nevercauses truncation of a value. Padding takes place only if the specified field width exceeds the length of the value. .PPIn all cases, the radix character .PN printfuses is defined by the last successful call to.PN setlocalecategory.PN LC_NUMERIC .If.PN setlocalecategory.PN LC_NUMERIChas not been called successfully or if the radix character is undefined,the radix character defaults to a period (.)..SS International Environment.IP LC_NUMERIC 15If this environment is set and valid, .PN printfuses the international language database named in the definition todetermine radix character rules..IP LANG 15If this environment variable is set and valid .PN printfuses the international language database named in the definition todetermine collation and character classification rules. If.PN LC_NUMERIC is defined, its definition supercedes the definition ofLANG..SH Restrictions.NXR "printf subroutine" "restrictions"The.PN printffunction cannot format values that exceed 128 characters..SH Examples.NXR "printf subroutine" "printing date"To print a date and time in the form Sunday, July 3, 10:02,where.I weekdayand.I monthare pointers to null-terminated strings use the following function call:.EXprintf("%s, %s %d, %02d:%02d", weekday, month, day, hour, min);.EETo print.if n pi.if t \(*pto 5 decimal places use the following call:.EXprintf("pi = %.5f", 4*atan(1.0));.EE.SH Return Values.NXR "printf subroutine" "System V and"In the BSD environment, .PN printf and .PN fprintf return zero for success and EOF for failure. The.PN sprintf subroutine returns its first argument for success and EOF for failure..PPIn the System V and POSIX environments, .PN printf ,.PN fprintf, and.PN sprintfreturn the number of characters transmitted for success. The.PN sprintffunction ignores the null terminator (\\0) when calculating the numberof characters transmitted. If an output error occurs, these routinesreturn a negative value..SH See Alsoecvt(3), nl_printf(3int), nl_scanf(3int), setlocale(3), putc(3s), scanf(3s), environ(5int).br .I Guide to Developing International Software
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -