?? library_11.html
字號(hào):
If the syntax of a conversion specification is invalid, unpredictable
things will happen, so don't do this. If there aren't enough function
arguments provided to supply values for all the conversion
specifications in the template string, or if the arguments are not of
the correct types, the results are unpredictable. If you supply more
arguments than conversion specifications, the extra argument values are
simply ignored; this is sometimes useful.
<P>
<H3><A NAME="SEC132" HREF="library_toc.html#SEC132" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC132">Integer Conversions</A></H3>
<P>
This section describes the options for the <SAMP>`%d'</SAMP>, <SAMP>`%i'</SAMP>,
<SAMP>`%o'</SAMP>, <SAMP>`%u'</SAMP>, <SAMP>`%x'</SAMP>, <SAMP>`%X'</SAMP>, and <SAMP>`%Z'</SAMP> conversion
specifications. These conversions print integers in various formats.
<P>
The <SAMP>`%d'</SAMP> and <SAMP>`%i'</SAMP> conversion specifications both print an
<CODE>int</CODE> argument as a signed decimal number; while <SAMP>`%o'</SAMP>,
<SAMP>`%u'</SAMP>, and <SAMP>`%x'</SAMP> print the argument as an unsigned octal,
decimal, or hexadecimal number (respectively). The <SAMP>`%X'</SAMP> conversion
specification is just like <SAMP>`%x'</SAMP> except that it uses the characters
<SAMP>`ABCDEF'</SAMP> as digits instead of <SAMP>`abcdef'</SAMP>. <SAMP>`%Z'</SAMP> is like
<SAMP>`%u'</SAMP> but expects an argument of type <CODE>size_t</CODE>.
<P>
The following flags are meaningful:
<P>
<DL COMPACT>
<DT><SAMP>`-'</SAMP>
<DD>Left-justify the result in the field (instead of the normal
right-justification).
<P>
<DT><SAMP>`+'</SAMP>
<DD>For the signed <SAMP>`%d'</SAMP> and <SAMP>`%i'</SAMP> conversions, print a
plus sign if the value is positive.
<P>
<DT><SAMP>` '</SAMP>
<DD>For the signed <SAMP>`%d'</SAMP> and <SAMP>`%i'</SAMP> conversions, if the result
doesn't start with a plus or minus sign, prefix it with a space
character instead. Since the <SAMP>`+'</SAMP> flag ensures that the result
includes 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>, this
prefixes 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 after
any 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 to
appear; leading zeros are produced if necessary. If you don't specify a
precision, the number is printed with as many digits as it needs. If
you convert a value of zero with an explicit precision of zero, then no
characters 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 friends
are variadic, any <CODE>char</CODE> and <CODE>short</CODE> arguments are
automatically converted to <CODE>int</CODE> by the default argument
promotions. For arguments of other integer types, you can use these
modifiers:
<P>
<DL COMPACT>
<DT><SAMP>`h'</SAMP>
<DD>Specifies that the argument is a <CODE>short int</CODE> or <CODE>unsigned
short 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 promotions
anyway, 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 long
int</CODE>, as appropriate.
<P>
<DT><SAMP>`L'</SAMP>
<DD>Specifies that the argument is a <CODE>long long int</CODE>. (This type is
an extension supported by the GNU C compiler. On systems that don't
support 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>, since
the 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 number
is too large to fit in the minimum field width specified.
<P>
Here are some more examples showing how unsigned integers print under
various 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-point
numbers: 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 controlled
by 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 by
the 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 style
of <SAMP>`%e'</SAMP> or <SAMP>`%E'</SAMP> (respectively) if the exponent would be less
than -4 or greater than or equal to the precision; otherwise they use the
<SAMP>`%f'</SAMP> style. Trailing zeros are removed from the fractional portion
of the result and a decimal-point character appears only if it is
followed 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 is
right-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 a
space instead. Since the <SAMP>`+'</SAMP> flag ensures that the result includes
a 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, even
if 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 left
in place where they would otherwise be removed.
<P>
<DT><SAMP>`0'</SAMP>
<DD>Pad the field with zeros instead of spaces; the zeros are placed
after any sign. This flag is ignored if the <SAMP>`-'</SAMP> flag is also
specified.
</DL>
<P>
The precision specifies how many digits follow the decimal-point
character for the <SAMP>`%f'</SAMP>, <SAMP>`%e'</SAMP>, and <SAMP>`%E'</SAMP> conversions. For
these conversions, the default is <CODE>6</CODE>. If the precision is
explicitly <CODE>0</CODE>, this has the rather strange effect of suppressing
the decimal point character entirely! For the <SAMP>`%g'</SAMP> and <SAMP>`%G'</SAMP>
conversions, the precision specifies how many significant digits to
print; 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 argument
of 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>long
double</CODE>.
</DL>
<P>
Here are some examples showing how numbers print using the various
floating-point conversions. All of the numbers were printed using
this 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 no
other 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 argument
must be of type <CODE>char *</CODE>. A precision can be specified to indicate
the maximum number of characters to write; otherwise characters in the
string up to but not including the terminating null character are
written to the output stream. The <SAMP>`-'</SAMP> flag can be used to specify
left-justification in the field, but no other flags or type modifiers
are 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 this
is more useful than crashing. But it's not good practice to pass a null
argument intentionally.
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -