📄 library_11.html
字号:
<P>The following flags are meaningful:<P><DL COMPACT><DT><SAMP>`-'</SAMP><DD>Left-justify the result in the field (instead of the normalright-justification).<P><DT><SAMP>`+'</SAMP><DD>For the signed <SAMP>`%d'</SAMP> and <SAMP>`%i'</SAMP> conversions, print aplus sign if the value is positive.<P><DT><SAMP>` '</SAMP><DD>For the signed <SAMP>`%d'</SAMP> and <SAMP>`%i'</SAMP> conversions, if the resultdoesn't start with a plus or minus sign, prefix it with a spacecharacter instead. Since the <SAMP>`+'</SAMP> flag ensures that the resultincludes a sign, this flag is ignored if you supply both of them.<P><DT><SAMP>`#'</SAMP><DD>For the <SAMP>`%o'</SAMP> conversion, this forces the leading digit to be <SAMP>`0'</SAMP>,as if by increasing the precision. For <SAMP>`%x'</SAMP> or <SAMP>`%X'</SAMP>, thisprefixes a leading <SAMP>`0x'</SAMP> or <SAMP>`0X'</SAMP> (respectively) to the result.This doesn't do anything useful for the <SAMP>`%d'</SAMP>, <SAMP>`%i'</SAMP>, or <SAMP>`%u'</SAMP>conversions.<P><DT><SAMP>`0'</SAMP><DD>Pad the field with zeros instead of spaces. The zeros are placed afterany indication of sign or base. This flag is ignored if the <SAMP>`-'</SAMP>flag is also specified, or if a precision is specified.</DL><P>If a precision is supplied, it specifies the minimum number of digits toappear; leading zeros are produced if necessary. If you don't specify aprecision, the number is printed with as many digits as it needs. Ifyou convert a value of zero with an explicit precision of zero, then nocharacters at all are produced.<P>Without a type modifier, the corresponding argument is treated as an<CODE>int</CODE> (for the signed conversions <SAMP>`%i'</SAMP> and <SAMP>`%d'</SAMP>) or<CODE>unsigned int</CODE> (for the unsigned conversions <SAMP>`%o'</SAMP>, <SAMP>`%u'</SAMP>,<SAMP>`%x'</SAMP>, and <SAMP>`%X'</SAMP>). Recall that since <CODE>printf</CODE> and friendsare variadic, any <CODE>char</CODE> and <CODE>short</CODE> arguments areautomatically converted to <CODE>int</CODE> by the default argumentpromotions. For arguments of other integer types, you can use thesemodifiers:<P><DL COMPACT><DT><SAMP>`h'</SAMP><DD>Specifies that the argument is a <CODE>short int</CODE> or <CODE>unsignedshort int</CODE>, as appropriate. A <CODE>short</CODE> argument is converted to an<CODE>int</CODE> or <CODE>unsigned int</CODE> by the default argument promotionsanyway, but the <SAMP>`h'</SAMP> modifier says to convert it back to a<CODE>short</CODE> again.<P><DT><SAMP>`l'</SAMP><DD>Specifies that the argument is a <CODE>long int</CODE> or <CODE>unsigned longint</CODE>, as appropriate. <P><DT><SAMP>`L'</SAMP><DD>Specifies that the argument is a <CODE>long long int</CODE>. (This type isan extension supported by the GNU C compiler. On systems that don'tsupport extra-long integers, this is the same as <CODE>long int</CODE>.)</DL><P>The modifiers for argument type are not applicable to <SAMP>`%Z'</SAMP>, sincethe sole purpose of <SAMP>`%Z'</SAMP> is to specify the data type<CODE>size_t</CODE>.<P>Here is an example. Using the template string:<P><PRE>|%5d|%-5d|%+5d|%+-5d|% 5d|%05d|%5.0d|%5.2d|%d|\n"</PRE><P>to print numbers using the different options for the <SAMP>`%d'</SAMP>conversion gives results like:<P><PRE>| 0|0 | +0|+0 | 0|00000| | 00|0|| 1|1 | +1|+1 | 1|00001| 1| 01|1|| -1|-1 | -1|-1 | -1|-0001| -1| -01|-1||100000|100000|+100000| 100000|100000|100000|100000|100000|</PRE><P>In particular, notice what happens in the last case where the numberis too large to fit in the minimum field width specified.<P>Here are some more examples showing how unsigned integers print undervarious format options, using the template string:<P><PRE>"|%5u|%5o|%5x|%5X|%#5o|%#5x|%#5X|%#10.8x|\n"</PRE><P><PRE>| 0| 0| 0| 0| 0| 0x0| 0X0|0x00000000|| 1| 1| 1| 1| 01| 0x1| 0X1|0x00000001||100000|303240|186a0|186A0|0303240|0x186a0|0X186A0|0x000186a0|</PRE><P><H3><A NAME="SEC133" HREF="library_toc.html#SEC133" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC133">Floating-Point Conversions</A></H3><P>This section discusses the conversion specifications for floating-pointnumbers: the <SAMP>`%f'</SAMP>, <SAMP>`%e'</SAMP>, <SAMP>`%E'</SAMP>, <SAMP>`%g'</SAMP>, and <SAMP>`%G'</SAMP>conversions.<P>The <SAMP>`%f'</SAMP> conversion prints its argument in fixed-point notation,producing output of the form[<CODE>-</CODE>]<VAR>ddd</VAR><CODE>.</CODE><VAR>ddd</VAR>,where the number of digits following the decimal point is controlledby the precision you specify.<P>The <SAMP>`%e'</SAMP> conversion prints its argument in exponential notation,producing output of the form[<CODE>-</CODE>]<VAR>d</VAR><CODE>.</CODE><VAR>ddd</VAR><CODE>e</CODE>[<CODE>+</CODE>|<CODE>-</CODE>]<VAR>dd</VAR>.Again, the number of digits following the decimal point is controlled bythe precision. The exponent always contains at least two digits. The<SAMP>`%E'</SAMP> conversion is similar but the exponent is marked with the letter<SAMP>`E'</SAMP> instead of <SAMP>`e'</SAMP>.<P>The <SAMP>`%g'</SAMP> and <SAMP>`%G'</SAMP> conversions print the argument in the styleof <SAMP>`%e'</SAMP> or <SAMP>`%E'</SAMP> (respectively) if the exponent would be lessthan -4 or greater than or equal to the precision; otherwise they use the<SAMP>`%f'</SAMP> style. Trailing zeros are removed from the fractional portionof the result and a decimal-point character appears only if it isfollowed by a digit.<P>The following flags can be used to modify the behavior:<P><DL COMPACT><DT><SAMP>`-'</SAMP><DD>Left-justify the result in the field. Normally the result isright-justified.<P><DT><SAMP>`+'</SAMP><DD>Always include a plus or minus sign in the result.<P><DT><SAMP>` '</SAMP><DD>If the result doesn't start with a plus or minus sign, prefix it with aspace instead. Since the <SAMP>`+'</SAMP> flag ensures that the result includesa sign, this flag is ignored if you supply both of them.<P><DT><SAMP>`#'</SAMP><DD>Specifies that the result should always include a decimal point, evenif no digits follow it. For the <SAMP>`%g'</SAMP> and <SAMP>`%G'</SAMP> conversions,this also forces trailing zeros after the decimal point to be leftin place where they would otherwise be removed.<P><DT><SAMP>`0'</SAMP><DD>Pad the field with zeros instead of spaces; the zeros are placedafter any sign. This flag is ignored if the <SAMP>`-'</SAMP> flag is alsospecified.</DL><P>The precision specifies how many digits follow the decimal-pointcharacter for the <SAMP>`%f'</SAMP>, <SAMP>`%e'</SAMP>, and <SAMP>`%E'</SAMP> conversions. Forthese conversions, the default is <CODE>6</CODE>. If the precision isexplicitly <CODE>0</CODE>, this has the rather strange effect of suppressingthe decimal point character entirely! For the <SAMP>`%g'</SAMP> and <SAMP>`%G'</SAMP>conversions, the precision specifies how many significant digits toprint; if <CODE>0</CODE> or not specified, it is treated like a value of<CODE>1</CODE>.<P>Without a type modifier, the floating-point conversions use an argumentof type <CODE>double</CODE>. (By the default argument promotions, any<CODE>float</CODE> arguments are automatically converted to <CODE>double</CODE>.)The following type modifier is supported:<P><DL COMPACT><DT><SAMP>`L'</SAMP><DD>An uppercase <SAMP>`L'</SAMP> specifies that the argument is a <CODE>longdouble</CODE>.</DL><P>Here are some examples showing how numbers print using the variousfloating-point conversions. All of the numbers were printed usingthis template string:<P><PRE>"|%12.4f|%12.4e|%12.4g|\n"</PRE><P>Here is the output:<P><PRE>| 0.0000| 0.0000e+00| 0|| 1.0000| 1.0000e+00| 1|| -1.0000| -1.0000e+00| -1|| 100.0000| 1.0000e+02| 100|| 1000.0000| 1.0000e+03| 1000|| 10000.0000| 1.0000e+04| 1e+04|| 12345.0000| 1.2345e+04| 1.234e+04|| 100000.0000| 1.0000e+05| 1e+05|| 123456.0000| 1.2346e+05| 1.234e+05|</PRE><P>Notice how the <SAMP>`%g'</SAMP> conversion drops trailing zeros.<P><H3><A NAME="SEC134" HREF="library_toc.html#SEC134" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC134">Other Output Conversions</A></H3><P>This section describes miscellaneous conversions for <CODE>printf</CODE>.<P>The <SAMP>`%c'</SAMP> conversion prints a single character. The <CODE>int</CODE>argument is first converted to an <CODE>unsigned char</CODE>. The <SAMP>`-'</SAMP>flag can be used to specify left-justification in the field, but noother flags are defined, and no precision or type modifier can be given.For example:<P><PRE>printf ("%c%c%c%c%c", 'h', 'e', 'l', 'l', 'o');</PRE><P>prints <SAMP>`hello'</SAMP>.<P>The <SAMP>`%s'</SAMP> conversion prints a string. The corresponding argumentmust be of type <CODE>char *</CODE>. A precision can be specified to indicatethe maximum number of characters to write; otherwise characters in thestring up to but not including the terminating null character arewritten to the output stream. The <SAMP>`-'</SAMP> flag can be used to specifyleft-justification in the field, but no other flags or type modifiersare defined for this conversion. For example:<P><PRE>printf ("%3s%-6s", "no", "where");</PRE><P>prints <SAMP>` nowhere '</SAMP>.<P>If you accidentally pass a null pointer as the argument for a <SAMP>`%s'</SAMP>conversion, the GNU library prints it as <SAMP>`(null)'</SAMP>. We think thisis more useful than crashing. But it's not good practice to pass a nullargument intentionally.<P>The <SAMP>`%m'</SAMP> conversion prints the string corresponding to the errorcode in <CODE>errno</CODE>. See section <A HREF="library_2.html#SEC17" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_2.html#SEC17">Error Messages</A>. Thus:<P><PRE>fprintf (stderr, "can't open `%s': %m\n", filename);</PRE><P>is equivalent to:<P><PRE>fprintf (stderr, "can't open `%s': %s\n", filename, strerror (errno));</PRE><P>The <SAMP>`%m'</SAMP> conversion is a GNU C library extension.<P>The <SAMP>`%p'</SAMP> conversion prints a pointer value. The correspondingargument must be of type <CODE>void *</CODE>. In practice, you can use anytype of pointer.<P>In the GNU system, non-null pointers are printed as unsigned integers,as if a <SAMP>`%#x'</SAMP> conversion were used. Null pointers print as<SAMP>`(nil)'</SAMP>. (Pointers might print differently in other systems.)<P>For example:<P><PRE>printf ("%p", "testing");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -