📄 library_11.html
字号:
<P>
The GNU C library only supports one character of pushback--in other
words, it does not work to call <CODE>ungetc</CODE> twice without doing input
in between. Other systems might let you push back multiple characters;
then reading from the stream retrieves the characters in the reverse
order that they were pushed.
<P>
Pushing back characters doesn't alter the file; only the internal
buffering for the stream is affected. If a file positioning function
(such as <CODE>fseek</CODE> or <CODE>rewind</CODE>; see section <A HREF="library_11.html#SEC158" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_11.html#SEC158">File Positioning</A>) is
called, any pending pushed-back characters are discarded.
<P>
Unreading a character on a stream that is at end of file clears the
end-of-file indicator for the stream, because it makes the character of
input available. Reading that character will set the end-of-file
indicator again.
<P>
Here is an example showing the use of <CODE>getc</CODE> and <CODE>ungetc</CODE> to
skip over whitespace characters. When this function reaches a
non-whitespace character, it unreads that character to be seen again on
the next read operation on the stream.
<P>
<PRE>
#include <stdio.h>
void
skip_whitespace (FILE *stream)
{
int c;
do
/* No need to check for <CODE>EOF</CODE> because it is not
<CODE>isspace</CODE>, and <CODE>ungetc</CODE> ignores <CODE>EOF</CODE>. */
c = getc (stream);
while (isspace (c));
ungetc (c, stream);
}
</PRE>
<P>
<H2><A NAME="SEC128" HREF="library_toc.html#SEC128" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC128">Formatted Output</A></H2>
<A NAME="IDX478"></A>
<A NAME="IDX479"></A>
<A NAME="IDX480"></A>
<A NAME="IDX481"></A>
<P>
The functions described in this section (<CODE>printf</CODE> and related
functions) provide a convenient way to perform formatted output. You
call <CODE>printf</CODE> with a <DFN>format string</DFN> or <DFN>template string</DFN>
that specifies how to format the values of the remaining arguments.
<P>
Unless your program is a filter that specifically performs line- or
character-oriented processing, using <CODE>printf</CODE> or one of the other
related functions described in this section is usually the easiest and
most concise way to perform output. These functions are especially
useful for printing error messages, tables of data, and the like.
<P>
<H3><A NAME="SEC129" HREF="library_toc.html#SEC129" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC129">Formatted Output Basics</A></H3>
<P>
The <CODE>printf</CODE> function can be used to print any number of arguments.
The template string argument you supply in a call provides
information not only about the number of additional arguments, but also
about their types and what style should be used for printing them.
<P>
Ordinary characters in the template string are simply written to the
output stream as-is, while <DFN>conversion specifications</DFN> introduced by
a <SAMP>`%'</SAMP> character in the template cause subsequent arguments to be
formatted and written to the output stream. For example,
<A NAME="IDX482"></A>
<P>
<PRE>
int pct = 37;
char filename[] = "foo.txt";
printf ("Processing of `%s' is %d%% finished.\nPlease be patient.\n",
filename, pct);
</PRE>
<P>
produces output like
<P>
<PRE>
Processing of `foo.txt' is 37% finished.
Please be patient.
</PRE>
<P>
This example shows the use of the <SAMP>`%d'</SAMP> conversion to specify that
an <CODE>int</CODE> argument should be printed in decimal notation, the
<SAMP>`%s'</SAMP> conversion to specify printing of a string argument, and
the <SAMP>`%%'</SAMP> conversion to print a literal <SAMP>`%'</SAMP> character.
<P>
There are also conversions for printing an integer argument as an
unsigned value in octal, decimal, or hexadecimal radix (<SAMP>`%o'</SAMP>,
<SAMP>`%u'</SAMP>, or <SAMP>`%x'</SAMP>, respectively); or as a character value
(<SAMP>`%c'</SAMP>).
<P>
Floating-point numbers can be printed in normal, fixed-point notation
using the <SAMP>`%f'</SAMP> conversion or in exponential notation using the
<SAMP>`%e'</SAMP> conversion. The <SAMP>`%g'</SAMP> conversion uses either <SAMP>`%e'</SAMP>
or <SAMP>`%f'</SAMP> format, depending on what is more appropriate for the
magnitude of the particular number.
<P>
You can control formatting more precisely by writing <DFN>modifiers</DFN>
between the <SAMP>`%'</SAMP> and the character that indicates which conversion
to apply. These slightly alter the ordinary behavior of the conversion.
For example, most conversion specifications permit you to specify a
minimum field width and a flag indicating whether you want the result
left- or right-justified within the field.
<P>
The specific flags and modifiers that are permitted and their
interpretation vary depending on the particular conversion. They're all
described in more detail in the following sections. Don't worry if this
all seems excessively complicated at first; you can almost always get
reasonable free-format output without using any of the modifiers at all.
The modifiers are mostly used to make the output look "prettier" in
tables.
<P>
<H3><A NAME="SEC130" HREF="library_toc.html#SEC130" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC130">Output Conversion Syntax</A></H3>
<P>
This section provides details about the precise syntax of conversion
specifications that can appear in a <CODE>printf</CODE> template
string.
<P>
Characters in the template string that are not part of a
conversion specification are printed as-is to the output stream.
Multibyte character sequences (see section <A HREF="library_6.html#SEC66" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_6.html#SEC66">Extended Characters</A>) are permitted in
a template string.
<P>
The conversion specifications in a <CODE>printf</CODE> template string have
the general form:
<P>
<PRE>
% <VAR>flags</VAR> <VAR>width</VAR> [ . <VAR>precision</VAR> ] <VAR>type</VAR> <VAR>conversion</VAR>
</PRE>
<P>
For example, in the conversion specifier <SAMP>`%-10.8ld'</SAMP>, the <SAMP>`-'</SAMP>
is a flag, <SAMP>`10'</SAMP> specifies the field width, the precision is
<SAMP>`8'</SAMP>, the letter <SAMP>`l'</SAMP> is a type modifier, and <SAMP>`d'</SAMP> specifies
the conversion style. (This particular type specifier says to
print a <CODE>long int</CODE> argument in decimal notation, with a minimum of
8 digits left-justified in a field at least 10 characters wide.)
<P>
In more detail, output conversion specifications consist of an
initial <SAMP>`%'</SAMP> character followed in sequence by:
<P>
<UL>
<LI>
Zero or more <DFN>flag characters</DFN> that modify the normal behavior of
the conversion specification.
<A NAME="IDX483"></A>
<P>
<LI>
An optional decimal integer specifying the <DFN>minimum field width</DFN>.
If the normal conversion produces fewer characters than this, the field
is padded with spaces to the specified width. This is a <EM>minimum</EM>
value; if the normal conversion produces more characters than this, the
field is <EM>not</EM> truncated. Normally, the output is right-justified
within the field.
<A NAME="IDX484"></A>
<P>
The GNU library's version of <CODE>printf</CODE> also allows you to specify a
field width of <SAMP>`*'</SAMP>. This means that the next argument in the
argument list (before the actual value to be printed) is used as the
field width. The value must be an <CODE>int</CODE>. Other C library versions may
not recognize this syntax.
<P>
<LI>
An optional <DFN>precision</DFN> to specify the number of digits to be
written for the numeric conversions. If the precision is specified, it
consists of a period (<SAMP>`.'</SAMP>) followed optionally by a decimal integer
(which defaults to zero if omitted).
<A NAME="IDX485"></A>
<P>
The GNU library's version of <CODE>printf</CODE> also allows you to specify a
precision of <SAMP>`*'</SAMP>. This means that the next argument in the
argument list (before the actual value to be printed) is used as the
precision. The value must be an <CODE>int</CODE>. If you specify <SAMP>`*'</SAMP>
for both the field width and precision, the field width argument
precedes the precision argument. Other C library versions may not
recognize this syntax.
<P>
<LI>
An optional <DFN>type modifier character</DFN>, which is used to specify the
data type of the corresponding argument if it differs from the default
type. (For example, the integer conversions assume a type of <CODE>int</CODE>,
but you can specify <SAMP>`h'</SAMP>, <SAMP>`l'</SAMP>, or <SAMP>`L'</SAMP> for other integer
types.)
<A NAME="IDX486"></A>
<P>
<LI>
A character that specifies the conversion to be applied.
</UL>
<P>
The exact options that are permitted and how they are interpreted vary
between the different conversion specifiers. See the descriptions of the
individual conversions for information about the particular options that
they use.
<P>
<A NAME="IDX487"></A>
<H3><A NAME="SEC131" HREF="library_toc.html#SEC131" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC131">Table of Output Conversions</A></H3>
<P>
Here is a table summarizing what all the different conversions do:
<P>
<DL COMPACT>
<DT><SAMP>`%d'</SAMP>, <SAMP>`%i'</SAMP>
<DD>Print an integer as a signed decimal number. See section <A HREF="library_11.html#SEC132" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_11.html#SEC132">Integer Conversions</A>, for details. <SAMP>`%d'</SAMP> and <SAMP>`%i'</SAMP> are synonymous for
output, but are different when used with <CODE>scanf</CODE> for input
(see section <A HREF="library_11.html#SEC148" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_11.html#SEC148">Table of Input Conversions</A>).
<P>
<DT><SAMP>`%o'</SAMP>
<DD>Print an integer as an unsigned octal number. See section <A HREF="library_11.html#SEC132" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_11.html#SEC132">Integer Conversions</A>, for details.
<P>
<DT><SAMP>`%u'</SAMP>
<DD>Print an integer as an unsigned decimal number. See section <A HREF="library_11.html#SEC132" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_11.html#SEC132">Integer Conversions</A>, for details.
<P>
<DT><SAMP>`%Z'</SAMP>
<DD>Print an integer as an unsigned decimal number, assuming it was passed
with type <CODE>size_t</CODE>. See section <A HREF="library_11.html#SEC132" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_11.html#SEC132">Integer Conversions</A>, for details.
<P>
<DT><SAMP>`%x'</SAMP>, <SAMP>`%X'</SAMP>
<DD>Print an integer as an unsigned hexadecimal number. <SAMP>`%x'</SAMP> uses
lower-case letters and <SAMP>`%X'</SAMP> uses upper-case. See section <A HREF="library_11.html#SEC132" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_11.html#SEC132">Integer Conversions</A>, for details.
<P>
<DT><SAMP>`%f'</SAMP>
<DD>Print a floating-point number in normal (fixed-point) notation.
See section <A HREF="library_11.html#SEC133" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_11.html#SEC133">Floating-Point Conversions</A>, for details.
<P>
<DT><SAMP>`%e'</SAMP>, <SAMP>`%E'</SAMP>
<DD>Print a floating-point number in exponential notation. <SAMP>`%e'</SAMP> uses
lower-case letters and <SAMP>`%E'</SAMP> uses upper-case. See section <A HREF="library_11.html#SEC133" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_11.html#SEC133">Floating-Point Conversions</A>, for details.
<P>
<DT><SAMP>`%g'</SAMP>, <SAMP>`%G'</SAMP>
<DD>Print a floating-point number in either normal or exponential notation,
whichever is more appropriate for its magnitude. <SAMP>`%g'</SAMP> uses
lower-case letters and <SAMP>`%G'</SAMP> uses upper-case. See section <A HREF="library_11.html#SEC133" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_11.html#SEC133">Floating-Point Conversions</A>, for details.
<P>
<DT><SAMP>`%c'</SAMP>
<DD>Print a single character. See section <A HREF="library_11.html#SEC134" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_11.html#SEC134">Other Output Conversions</A>.
<P>
<DT><SAMP>`%s'</SAMP>
<DD>Print a string. See section <A HREF="library_11.html#SEC134" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_11.html#SEC134">Other Output Conversions</A>.
<P>
<DT><SAMP>`%p'</SAMP>
<DD>Print the value of a pointer. See section <A HREF="library_11.html#SEC134" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_11.html#SEC134">Other Output Conversions</A>.
<P>
<DT><SAMP>`%n'</SAMP>
<DD>Get the number of characters printed so far. See section <A HREF="library_11.html#SEC134" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_11.html#SEC134">Other Output Conversions</A>.
Note that this conversion specification never produces any output.
<P>
<DT><SAMP>`%m'</SAMP>
<DD>Print the string corresponding to the value of <CODE>errno</CODE>.
See section <A HREF="library_11.html#SEC134" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_11.html#SEC134">Other Output Conversions</A>.
<P>
<DT><SAMP>`%%'</SAMP>
<DD>Print a literal <SAMP>`%'</SAMP> character. See section <A HREF="library_11.html#SEC134" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_11.html#SEC134">Other Output Conversions</A>.
</DL>
<P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -