📄 lib_prin.html
字号:
%hd int x (short)x 10 1 %ld long x (long)x 10 1 %e double x (double)x 10 6 %Le long double x (long double)x 10 6 %E double x (double)x 10 6 %LE long double x (long double)x 10 6 %f double x (double)x 10 6 %Lf long double x (long double)x 10 6 %g double x (double)x 10 6 %Lg long double x (long double)x 10 6 %G double x (double)x 10 6 %LG long double x (long double)x 10 6 %i int x (int)x 10 1 %hi int x (short)x 10 1 %li long x (long)x 10 1 %n int *x %hn short *x %ln long *x %o int x (unsigned int)x 8 1 %ho int x (unsigned short)x 8 1 %lo long x (unsigned long)x 8 1 %p void *x (void *)x %s char x[] x[0]... <B>large</B> %u int x (unsigned int)x 10 1 %hu int x (unsigned short)x 10 1 %lu long x (unsigned long)x 10 1 %x int x (unsigned int)x 16 1 %hx int x (unsigned short)x 16 1 %lx long x (unsigned long)x 16 1 %X int x (unsigned int)x 16 1 %hX int x (unsigned short)x 16 1 %lX long x (unsigned long)x 16 1 %% <B>none</B> '%'</PRE><P>The print conversion specifier determines any behavior not summarizedin this table. For all floating-point conversions:</P><UL><LI>Positive infinity prints as <CODE>inf</CODE> or <CODE>INF</CODE>.</LI><LI>Negative infinity prints as <CODE>-inf</CODE> or <CODE>-INF</CODE>.</LI><LI>Not-a-number (NaN) prints as <CODE>nan</CODE> or <CODE>NAN</CODE>.</LI></UL><P>The upper-case version prints only for an upper-case conversion specifier,such as <CODE>%E</CODE> but not <CODE>%Lg</CODE>.</P><P>In the following descriptions, <I>p</I> is the precision.Examples follow each of the print conversion specifiers.A single conversion can generate up to 509 characters.</P><P>You write <B><A NAME="%c"><CODE>%c</CODE></A></B>to generate a single character from the convertedvalue.</P><PRE> printf("%c", 'a') <B>generates a</B> printf("<%3c|%-3c>", 'a', 'b') <B>generates < a|b ></B></PRE><P>You write <B><A NAME="%d"><CODE>%d</CODE></A></B>,<B><A NAME="%i"><CODE>%i</CODE></A></B>,<B><A NAME="%o"><CODE>%o</CODE></A></B>,<B><A NAME="%u"><CODE>%u</CODE></A></B>,<B><A NAME="%x"><CODE>%x</CODE></A></B>, or<B><A NAME="%X"><CODE>%X</CODE></A></B> to generatea possibly signed integer representation. <CODE>%d</CODE> or <CODE>%i</CODE>specifies signed decimal representation, <CODE>%o</CODE>unsigned octal, <CODE>%u</CODE> unsigned decimal,<CODE>%x</CODE> unsigned hexadecimal using the digits <CODE>0-9</CODE>and <CODE>a-f</CODE>, and <CODE>%X</CODE> unsignedhexadecimal using the digits <CODE>0-9</CODE> and <CODE>A-F</CODE>.The conversion generates at least <I>p</I> digitsto represent the converted value. If <I>p</I> is zero,a converted value of zero generates no digits.</P><PRE> printf("%d %o %x", 31, 31, 31) <B>generates 31 37 1f</B> printf("%hu", 0xffff) <B>generates 65535</B> printf("%#X %+d", 31, 31) <B>generates 0X1F +31</B></PRE><P>You write <B><A NAME="%e"><CODE>%e</CODE></A></B> or<B><A NAME="%E"><CODE>%E</CODE></A></B> to generatea signed decimal fractional representation witha decimal power-of-ten exponent. The generated text takes the form<I>±d.dddE±dd,</I> where <I>±</I> is either a plus orminus sign, <I>d</I> is a decimal digit, the decimal point (<I>.</I>)is the decimal point for the current<A HREF="locale.html#locale">locale</A>,and <I>E</I> is either <CODE>e</CODE> (for <CODE>%e</CODE> conversion)or <CODE>E</CODE> (for <CODE>%E</CODE> conversion).The generated text has one integer digit,a decimal point if <I>p</I> is nonzero or if you specify the <CODE>#</CODE>format flag, <I>p</I> fraction digits, and at least two exponent digits.The result is rounded. The value zero has a zero exponent.</P><PRE> printf("%e", 31.4) <B>generates 3.140000e+01</B> printf("%.2E", 31.4) <B>generates 3.14E+01</B></PRE><P>You write <B><A NAME="%f"><CODE>%f</CODE></A></B>to generate a signed decimal fractional representationwith no exponent. The generated text takes the form <I>±d.ddd,</I>where <I>±</I> is either a plus or minus sign,<I>d</I> is a decimal digit, and the decimal point (<I>.</I>)is the decimal point for the current<A HREF="locale.html#locale">locale</A>.The generated text has at least one integer digit,a decimal point if <I>p</I> is nonzero or if you specifythe <CODE>#</CODE> format flag, and <I>p</I> fraction digits.The result is rounded.</P><PRE> printf("%f", 31.4) <B>generates 31.400000</B> printf("%.0f %#.0f", 31.0, 31.0)<B>generates 31 31.</B></PRE><P>You write <B><A NAME="%g"><CODE>%g</CODE></A></B> or<B><A NAME="%G"><CODE>%G</CODE></A></B> to generatea signed decimal fractionalrepresentation with or without a decimal power-of-ten exponent, as appropriate.For <CODE>%g</CODE> conversion, the generated texttakes the same form as either <CODE>%e</CODE>or <CODE>%f</CODE> conversion. For <CODE>%G</CODE> conversion,it takes the same form as either<CODE>%E</CODE> or<CODE>%f</CODE>conversion.The precision <I>p</I> specifies the numberof significant digits generated. (If <I>p</I> is zero, it is changedto 1.) If <CODE>%e</CODE> conversion would yield an exponent in the range[-4, <I>p</I>), then <CODE>%f</CODE> conversion occurs instead.The generated text has no trailing zerosin any fraction and has a decimal pointonly if there are nonzero fraction digits, unless you specify the<CODE>#</CODE> format flag.</P><PRE> printf("%.6g", 31.4) <B>generates 31.4</B> printf("%.1g", 31.4) <B>generates 3.14e+01</B></PRE><P>You write <B><A NAME="%n"><CODE>%n</CODE></A></B>to store the number of characters generated(up to this point in the format) in an integer objectwhose address is the value of the next successive argument.</P><PRE> printf("abc%n", &x) <B>stores 3</B></PRE><P>You write <B><A NAME="%p"><CODE>%p</CODE></A></B>to generate an external representation of a <I>pointer to void.</I>The conversion is implementation defined.</P><PRE> printf("%p", (void *)&x) <B>generates, e.g. F4C0</B></PRE><P>You write <B><A NAME="%s"><CODE>%s</CODE></A></B>to generate a sequence of characters fromthe values stored in the argument<A HREF="lib_over.html#C string">C string</A>.</P><PRE> printf("%s", "hello") <B>generates hello</B> printf("%.2s", "hello") <B>generates he</B></PRE><P>You write <B><A NAME="%%"><CODE>%%</CODE></A></B> to generatethe percent character (<CODE>%</CODE>).</P><PRE> printf("%%") <B>generates %</B></PRE><HR><P>See also the<B><A HREF="index.html#Table of Contents">Table of Contents</A></B> and the<B><A HREF="_index.html">Index</A></B>.</P><P><I><A HREF="crit_pb.html">Copyright</A> © 1989-2002by P.J. Plauger and Jim Brodie. All rights reserved.</I></P><!--V4.01:1125--></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -