📄 library_11.html
字号:
input available. Reading that character will set the end-of-fileindicator again.<P>Here is an example showing the use of <CODE>getc</CODE> and <CODE>ungetc</CODE> toskip over whitespace characters. When this function reaches anon-whitespace character, it unreads that character to be seen again onthe next read operation on the stream.<P><PRE>#include <stdio.h>voidskip_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 relatedfunctions) provide a convenient way to perform formatted output. Youcall <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- orcharacter-oriented processing, using <CODE>printf</CODE> or one of the otherrelated functions described in this section is usually the easiest andmost concise way to perform output. These functions are especiallyuseful 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 providesinformation not only about the number of additional arguments, but alsoabout their types and what style should be used for printing them.<P>Ordinary characters in the template string are simply written to theoutput stream as-is, while <DFN>conversion specifications</DFN> introduced bya <SAMP>`%'</SAMP> character in the template cause subsequent arguments to beformatted 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 thatan <CODE>int</CODE> argument should be printed in decimal notation, the<SAMP>`%s'</SAMP> conversion to specify printing of a string argument, andthe <SAMP>`%%'</SAMP> conversion to print a literal <SAMP>`%'</SAMP> character.<P>There are also conversions for printing an integer argument as anunsigned 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 notationusing 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 themagnitude 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 conversionto apply. These slightly alter the ordinary behavior of the conversion.For example, most conversion specifications permit you to specify aminimum field width and a flag indicating whether you want the resultleft- or right-justified within the field.<P>The specific flags and modifiers that are permitted and theirinterpretation vary depending on the particular conversion. They're alldescribed in more detail in the following sections. Don't worry if thisall seems excessively complicated at first; you can almost always getreasonable free-format output without using any of the modifiers at all.The modifiers are mostly used to make the output look "prettier" intables.<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 conversionspecifications that can appear in a <CODE>printf</CODE> templatestring.<P>Characters in the template string that are not part of aconversion 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 ina template string.<P>The conversion specifications in a <CODE>printf</CODE> template string havethe 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> specifiesthe conversion style. (This particular type specifier says toprint a <CODE>long int</CODE> argument in decimal notation, with a minimum of8 digits left-justified in a field at least 10 characters wide.)<P>In more detail, output conversion specifications consist of aninitial <SAMP>`%'</SAMP> character followed in sequence by:<P><UL><LI>Zero or more <DFN>flag characters</DFN> that modify the normal behavior ofthe 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 fieldis padded with spaces to the specified width. This is a <EM>minimum</EM>value; if the normal conversion produces more characters than this, thefield is <EM>not</EM> truncated. Normally, the output is right-justifiedwithin the field.<A NAME="IDX484"></A><P>The GNU library's version of <CODE>printf</CODE> also allows you to specify afield width of <SAMP>`*'</SAMP>. This means that the next argument in theargument list (before the actual value to be printed) is used as thefield width. The value must be an <CODE>int</CODE>. Other C library versions maynot recognize this syntax.<P><LI>An optional <DFN>precision</DFN> to specify the number of digits to bewritten for the numeric conversions. If the precision is specified, itconsists 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 aprecision of <SAMP>`*'</SAMP>. This means that the next argument in theargument list (before the actual value to be printed) is used as theprecision. The value must be an <CODE>int</CODE>. If you specify <SAMP>`*'</SAMP>for both the field width and precision, the field width argumentprecedes the precision argument. Other C library versions may notrecognize this syntax.<P><LI>An optional <DFN>type modifier character</DFN>, which is used to specify thedata type of the corresponding argument if it differs from the defaulttype. (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 integertypes.)<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 theindividual conversions for information about the particular options thatthey 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 foroutput, 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 passedwith 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> useslower-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> useslower-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> useslower-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>If the syntax of a conversion specification is invalid, unpredictablethings will happen, so don't do this. If there aren't enough functionarguments provided to supply values for all the conversionspecifications in the template string, or if the arguments are not ofthe correct types, the results are unpredictable. If you supply morearguments than conversion specifications, the extra argument values aresimply 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> conversionspecifications. 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> conversionspecification 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>.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -