⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 appb.html

📁 Kernighan and Ritchie - The C Programming Language c程序设计语言(第二版)称作是C语言学习的圣经
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<html><head><title>Appendix B - Standard Library</title></head><body><hr><p align="center"><a href="appa.html">Back to Appendix A</a>&nbsp;--&nbsp;<a href="kandr.html">Index</a>&nbsp;--&nbsp;<a href="appc.html">Appendix C</a><p><hr><h1>Appendix B - Standard Library</h1>This appendix is a summary of the library defined by the ANSI standard. Thestandard library is not part of the C language proper, but an environmentthat supports standard C will provide the function declarations and type andmacro definitions of this library. We have omitted a few functions that areof limited utility or easily synthesized from others; we have omittedmulti-byte characters; and we have omitted discussion of locale issues; thatis, properties that depend on local language, nationality, or culture.<p>The functions, types and macros of the standard library are declared instandard <em>headers</em>:<pre>   &lt;assert.h&gt;  &lt;float.h&gt;   &lt;math.h&gt;    &lt;stdarg.h&gt;  &lt;stdlib.h&gt;   &lt;ctype.h&gt;   &lt;limits.h&gt;  &lt;setjmp.h&gt;  &lt;stddef.h&gt;  &lt;string.h&gt;   &lt;errno.h&gt;   &lt;locale.h&gt;  &lt;signal.h&gt;  &lt;stdio.h&gt;   &lt;time.h&gt;</pre>A header can be accessed by<p>&nbsp;&nbsp;<tt>#include</tt> &lt;<em>header</em>&gt;<p>Headers may be included in any order and any number of times. A header mustbe included outside of any external declaration or definition and before anyuse of anything it declares. A header need not be a source file.<p>External identifiers that begin with an underscore are reserved for use bythe library, as are all other identifiers that begin with an underscore andan upper-case letter or another underscore.<h2><a name="sb.1">B.1 Input and Output: &lt;stdio.h&gt;</a></h2>The input and output functions, types, and macros defined in<tt>&lt;stdio.h&gt;</tt> represent nearly one third of the library.<p>A <em>stream</em> is a source or destination of data that may be associatedwith a disk or other peripheral. The library supports text streams and binarystreams, although on some systems, notably UNIX, these are identical. A textstream is a sequence of lines; each line has zero or more characters and isterminated by <tt>'\n'</tt>. An environment may need to convert a text streamto or from some other representation (such as mapping <tt>'\n'</tt> tocarriage return and linefeed). A binary stream is a sequence of unprocessedbytes that record internal data, with the property that if it is written,then read back on the same system, it will compare equal.<p>A stream is connected to a file or device by <em>opening</em> it; theconnection is broken by <em>closing</em> the stream. Opening a file returnsa pointer to an object of type <tt>FILE</tt>, which records whateverinformation is necessary to control the stream. We will use ``file pointer''and ``stream'' interchangeably when there is no ambiguity.<p>When a program begins execution, the three streams <tt>stdin</tt>,<tt>stdout</tt>, and <tt>stderr</tt> are already open.<h3><a name="sb.1.1">B.1.1 File Operations</a></h3>The following functions deal with operations on files. The type<tt>size_t</tt> is the unsigned integral type produced by the <tt>sizeof</tt>operator.<p><dl><p><dt><tt>FILE *fopen(const char *filename, const char *mode)</tt><dd><tt>fopen</tt> opens the named file, and returns a stream, or    <tt>NULL</tt> if the attempt fails. Legal values for <tt>mode</tt>    include:<p><table align="center"><td><tt>"r"</tt></td><td>open text file for reading</td><tr><td><tt>"w"</tt></td><td>create text file for writing; discard previous contents if any</td><tr><td><tt>"a"</tt></td><td>append; open or create text file for writing at end of file</td><tr><td><tt>"r+"</tt></td><td>open text file for update (i.e., reading and writing)</td><tr><td><tt>"w+"</tt></td><td>create text file for update, discard previous contents if any</td><tr><td><tt>"a+"</tt></td><td>append; open or create text file for update, writing at end </td></table><p><dd>Update mode permits reading and writing the same file; <tt>fflush</tt> or    a file-positioning function must be called between a read and a write or    vice versa. If the mode includes <tt>b</tt> after the initial letter, as    in <tt>"rb"</tt> or <tt>"w+b"</tt>, that indicates a binary file.    Filenames are limited to <tt>FILENAME_MAX</tt> characters. At most    <tt>FOPEN_MAX</tt> files may be open at once.<p><dt><tt>FILE *freopen(const char *filename, const char *mode, FILE *stream)</tt><dd><tt>freopen</tt> opens the file with the specified mode and associates    the stream with it. It returns <tt>stream</tt>, or <tt>NULL</tt> if an    error occurs. <tt>freopen</tt> is normally used to change the files    associated with <tt>stdin</tt>, <tt>stdout</tt>, or <tt>stderr</tt>.<p><dt><tt>int fflush(FILE *stream)</tt><dd>On an output stream, <tt>fflush</tt> causes any buffered but unwritten    data to be written; on an input stream, the effect is undefined. It    returns <tt>EOF</tt> for a write error, and zero otherwise.    <tt>fflush(NULL)</tt> flushes all output streams.<p><dt><tt>int fclose(FILE *stream)</tt><dd><tt>fclose</tt> flushes any unwritten data for <tt>stream</tt>, discards    any unread buffered input, frees any automatically allocated buffer, then    closes the stream. It returns <tt>EOF</tt> if any errors occurred, and    zero otherwise.<p><dt><tt>int remove(const char *filename)</tt><dd><tt>remove</tt> removes the named file, so that a subsequent attempt to    open it will fail. It returns non-zero if the attempt fails.<p><dt><tt>int rename(const char *oldname, const char *newname)</tt><dd><tt>rename</tt> changes the name of a file; it returns non-zero if the    attempt fails.<p><dt><tt>FILE *tmpfile(void)</tt><dd><tt>tmpfile</tt> creates a temporary file of mode <tt>"wb+"</tt> that will be    automatically removed when closed or when the program terminates normally.    <tt>tmpfile</tt> returns a stream, or <tt>NULL</tt> if it could not create the file.<p><dt><tt>char *tmpnam(char s[L_tmpnam])</tt><dd><tt>tmpnam(NULL)</tt> creates a string that is not the name of an    existing file, and returns a pointer to an internal static array.    <tt>tmpnam(s)</tt> stores the string in <tt>s</tt> as well as returning    it as the function value; <tt>s</tt> must have room for at least    <tt>L_tmpnam</tt> characters. <tt>tmpnam</tt> generates a different name    each time it is called; at most <tt>TMP_MAX</tt> different names are    guaranteed during execution of the program. Note that <tt>tmpnam</tt>    creates a name, not a file.<p><dt><tt>int setvbuf(FILE *stream, char *buf, int mode, size_t size)</tt><dd><tt>setvbuf</tt> controls buffering for the stream; it must be called    before reading, writing or any other operation. A <tt>mode</tt> of <tt>_IOFBF</tt>    causes full buffering, <tt>_IOLBF</tt> line buffering of text files, and    <tt>_IONBF</tt> no buffering. If <tt>buf</tt> is not <tt>NULL</tt>, it    will be used as the buffer, otherwise a buffer will be allocated.    <tt>size</tt> determines the buffer size. <tt>setvbuf</tt> returns    non-zero for any error.<p><dt><tt>void setbuf(FILE *stream, char *buf)</tt><dd>If <tt>buf</tt> is <tt>NULL</tt>, buffering is turned off for the stream.    Otherwise, <tt>setbuf</tt> is equivalent to    <tt>(void) setvbuf(stream, buf, _IOFBF, BUFSIZ)</tt>.</dl><h3><a name="sb.1.2">B.1.2 Formatted Output</a></h3>The <tt>printf</tt> functions provide formatted output conversion.<pre>   int fprintf(FILE *stream, const char *format, ...)</pre><tt>fprintf</tt> converts and writes output to <tt>stream</tt> under thecontrol of <tt>format</tt>. The return value is the number of characterswritten, or negative if an error occurred.<p>The format string contains two types of objects: ordinary characters, whichare copied to the output stream, and conversion specifications, each of whichcauses conversion and printing of the next successive argument to<tt>fprintf</tt>. Each conversion specification begins with the character<tt>%</tt> and ends with a conversion character. Between the <tt>%</tt> andthe conversion character there may be, in order:<p><ul><li>Flags (in any order), which modify the specification:      <ul>      <li><tt>-</tt>, which specifies left adjustment of the converted          argument in its field.      <li><tt>+</tt>, which specifies that the number will always be printed          with a sign.      <li><em>space</em>: if the first character is not a sign, a space will          be prefixed.      <li><tt>0</tt>: for numeric conversions, specifies padding to the field          width with leading zeros.      <li><tt>#</tt>, which specifies an alternate output form. For <tt>o</tt>,          the first digit will become zero. For <tt>x</tt> or <tt>X</tt>,          <tt>0x</tt> or <tt>0X</tt> will be prefixed to a non-zero result.          For <tt>e</tt>, <tt>E</tt>, <tt>f</tt>, <tt>g</tt>, and <tt>G</tt>,          the output will always have a decimal point; for <tt>g</tt> and          <tt>G</tt>, trailing zeros will not be removed.      </ul><li>A number specifying a minimum field width. The converted argument will    be printed in a field at least this wide, and wider if necessary. If the    converted argument has fewer characters than the field width it will be    padded on the left (or right, if left adjustment has been requested) to    make up the field width. The padding character is normally space, but is    <tt>0</tt> if the zero padding flag is present.<li>A period, which separates the field width from the precision.<li>A number, the precision, that specifies the maximum number of characters    to be printed from a string, or the number of digits to be printed after    the decimal point for <tt>e</tt>, <tt>E</tt>, or <tt>f</tt> conversions,    or the number of significant digits for <tt>g</tt> or <tt>G</tt>    conversion, or the number of digits to be printed for an integer (leading    <tt>0</tt>s will be added to make up the necessary width).<li>A length modifier <tt>h</tt>, <tt>l</tt> (letter ell), or <tt>L</tt>.    ``<tt>h</tt>'' indicates that the corresponding argument is to be printed    as a <tt> short</tt> or <tt>unsigned short</tt>; ``<tt>l</tt>'' indicates    that the argument is a <tt>long</tt> or <tt>unsigned long</tt>,    ``<tt>L</tt>'' indicates that the argument is a <tt>long double</tt>.</ul><p>Width or precision or both may be specified as <tt>*</tt>, in which case the valueis computed by converting the next argument(s), which must be <tt>int</tt>.<p>The conversion characters and their meanings are shown in Table B.1.If the character after the <tt>%</tt> is not a conversioncharacter, the behavior is undefined.<p align="center"><em><strong>Table B.1</strong> Printf Conversions</em><p align="center"><table border=1><th align="center">Character<th align="center">Argument type; Printed As<tr><td><tt>d,i</tt></td><td><tt>int</tt>; signed decimal notation.</td><tr><td><tt>o</tt></td><td><tt>int</tt>; unsigned octal notation (without a leading zero).</td><tr><td><tt>x,X</tt></td><td><tt>unsigned int</tt>; unsigned hexadecimal notation                 (without a leading <tt>0x</tt> or <tt>0X</tt>), using		 <tt>abcdef</tt> for <tt>0x</tt> or <tt>ABCDEF</tt>		 for <tt>0X</tt>.</td><tr><td><tt>u</tt></td><td><tt>int</tt>; unsigned decimal notation.</td><tr><td><tt>c</tt></td><td><tt>int</tt>; single character, after conversion to	       <tt>unsigned char</tt></td><tr><td><tt>s</tt></td><td><tt>char *</tt>; characters from the string are	       printed until a <tt>'\0'</tt> is reached or until the	       number of characters indicated by the precision have	       been printed.</td><tr><td><tt>f</tt></td><td><tt>double</tt>; decimal notation of the form	       <tt>[-]</tt><em>mmm.ddd</em>, where the number of	       <em>d</em>'s is given by the precision. The default	       precision is 6; a precision of 0 suppresses the	       decimal point.</td><tr><td><tt>e,E</tt></td><td><tt>double</tt>; decimal notation of the form		 <tt>[-]</tt><em>m.dddddd</em><tt>e+/-</tt><em>xx</em> or		 <tt>[-]</tt><em>m.dddddd</em><tt>E+/-</tt><em>xx</em>,		 where the number of <em>d</em>'s is specified by the		 precision. The default precision is 6; a precision		 of 0 suppresses the decimal point.</td><tr><td><tt>g,G</tt></td><td><tt>double</tt>; <tt>%e</tt> or <tt>%E</tt> is used		 if the exponent is less than -4 or greater than or		 equal to the precision; otherwise <tt>%f</tt> is		 used. Trailing zeros and a trailing decimal point		 are not printed.</td><tr><td><tt>p</tt></td><td><tt>void *</tt>; print as a pointer  (implementation-dependent representation).</td><tr><td><tt>n</tt></td><td><tt>int *</tt>; the number of characters written so	       far by this call to <tt>printf</tt> is <em>written	       into</em> the argument. No argument is converted.</td><tr><td><tt>%</tt></td><td>no argument is converted; print a %</td></table><p><dl><p><dt><tt>int printf(const char *format, ...)</tt><dd><tt>printf(...)</tt> is equivalent to <tt>fprintf(stdout, ...)</tt>.<p><dt><tt>int sprintf(char *s, const char *format, ...)</tt><dd><tt>sprintf</tt> is the same as <tt>printf</tt> except that the output is writteninto the string <tt>s</tt>, terminated with <tt>'\0'</tt>. <tt>s</tt> must bebig enough to hold the result. The return count does not include the<tt>'\0'</tt>.<p><dt><tt>int vprintf(const char *format, va_list arg)</tt><br><tt>int vfprintf(FILE *stream, const char *format, va_list arg)</tt><br><tt>int vsprintf(char *s, const char *format, va_list arg)</tt><dd>The functions <tt>vprintf</tt>, <tt>vfprintf</tt>, and <tt>vsprintf</tt>are equivalent to the corresponding <tt>printf</tt> functions, exceptthat the variable argument list is replaced by <tt>arg</tt>, which hasbeen initialized by the <tt>va_start</tt> macro and perhaps<tt>va_arg</tt> calls. See the discussion of <tt>&lt;stdarg.h&gt;</tt> in<a href="appb.html#sb.7">Section B.7</a>.</dl><h3><a name="sb.1.3">B.1.3 Formatted Input</a></h3>The <tt>scanf</tt> function deals with formatted input conversion.<pre>int fscanf(FILE *stream, const char *format, ...)</pre><tt>fscanf</tt> reads from <tt>stream</tt> under control of <tt>format</tt>,and assigns converted values through subsequent arguments, <em>each of which

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -