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

📄 appb.html

📁 Kernighan and Ritchie - The C Programming Language c程序设计语言(第二版)称作是C语言学习的圣经
💻 HTML
📖 第 1 页 / 共 4 页
字号:
must be a pointer</em>. It returns when <tt>format</tt> is exhausted.<tt>fscanf</tt> returns <tt>EOF</tt> if end of file or an error occurs beforeany conversion; otherwise it returns the number of input items converted andassigned.<p>The format string usually contains conversion specifications, which are usedto direct interpretation of input. The format string may contain:<ul><li>Blanks or tabs, which are not ignored.<li>Ordinary characters (not %), which are expected to match the nextnon-white space character of the input stream.<li>Conversion specifications, consisting of a <tt>%</tt>, an optionalassignment suppression character <tt>*</tt>, an optional number specifyinga maximum field width, an optional <tt>h</tt>, <tt>l</tt>, or <tt>L</tt>indicating the width of the target, and a conversion character.</ul>A conversion specification determines the conversion of the next input field.Normally the result is placed in the variable pointed to by the correspondingargument. If assignment suppression is indicated by <tt>*</tt>, as in<tt>%*s</tt>, however, the input field is simply skipped; no assignment is made.An input field is defined as a string of non-white space characters; itextends either to the next white space character or until the field width, ifspecified, is exhausted. This implies that <tt>scanf</tt> will read across lineboundaries to find its input, since newlines are white space. (White spacecharacters are blank, tab, newline, carriage return, vertical tab, andformfeed.)<p>The conversion character indicates the interpretation of the input field. Thecorresponding argument must be a pointer. The legal conversion characters areshown in Table B.2.<p>The conversion characters <tt>d</tt>, <tt>i</tt>, <tt>n</tt>, <tt>o</tt>,<tt>u</tt>, and <tt>x</tt> may be preceded by <tt>h</tt> if the argument is apointer to <tt>short</tt> rather than int, or by <tt>l</tt> (letter ell) ifthe argument is a pointer to <tt>long</tt>. The conversion characters<tt>e</tt>, <tt>f</tt>, and <tt>g</tt> may be preceded by <tt>l</tt> if apointer to <tt>double</tt> rather than <tt>float</tt> is in the argumentlist, and by <tt>L</tt> if a pointer to a <tt>long double</tt>.<p align="center"><em><strong>Table B.2</strong> Scanf Conversions</em><p align="center"><table border=1><th align="center">Character<th align="center">Input Data; Argument type<tr><td><tt>d</tt></td><td>decimal integer; <tt>int*</tt><tr><td><tt>i</tt></td><td>integer; <tt>int*</tt>. The integer may be in octal	       (leading <tt>0</tt>) or hexadecimal (leading	       <tt>0x</tt> or <tt>0X</tt>).<tr><td><tt>o</tt></td><td>octal integer (with or without leading zero); <tt>int *</tt>.<tr><td><tt>u</tt></td><td>unsigned decimal integer; <tt>unsigned int *</tt>.<tr><td><tt>x</tt></td><td>hexadecimal integer (with or without leading	       <tt>0x</tt> or <tt>0X</tt>); <tt>int*</tt>.<tr><td><tt>c</tt></td><td>characters; <tt>char*</tt>. The next input characters	       are placed in the indicated array, up to the number	       given by the width field; the default is 1. No	       <tt>'\0'</tt> is added. The normal skip over white	       space characters is suppressed in this case; to read	       the next non-white space character, use <tt>%1s</tt>.<tr><td><tt>s</tt></td><td>string of non-white space characters (not quoted);	       <tt>char *</tt>, pointing to an array of characters	       large enough to hold the string and a terminating	       <tt>'\0'</tt> that will be added.<tr><td><tt>e,f,g</tt></td><td>floating-point number; <tt>float *</tt>. The input		   format for <tt>float</tt>'s is an optional sign, a		   string of numbers possibly containing a decimal		   point, and an optional exponent field containing		   an <tt>E</tt> or <tt>e</tt> followed by a possibly		   signed integer.<tr><td><tt>p</tt></td><td>pointer value as printed by <tt>printf("%p");</tt>, <tt>void *</tt>.<tr><td><tt>n</tt></td><td>writes into the argument the number  of characters	       read so far by this call; <tt>int *</tt>. No input is	       read. The converted item count is not incremented.<tr><td><tt>[...]</tt></td><td>matches the longest non-empty string of input		   characters from the set between brackets; <tt>char		   *</tt>. A <tt>'\0'</tt> is added. <tt>[]...]</tt>		   includes <tt>]</tt> in the set.<tr><td><tt>[^...]</tt></td><td>matches the longest non-empty string of input		    characters <em> not</em> from the set between		    brackets; <tt>char *</tt>. A <tt>'\0'</tt> is		    added. <tt>[^]...]</tt> includes <tt>]</tt> in		    the set.<tr><td><tt>%</tt></td><td>literal %; no assignment is made.</td></table><p><dl><p><dt><tt>int scanf(const char *format, ...)</tt><dd><tt>scanf(...)</tt> is identical to <tt>fscanf(stdin, ...)</tt>.<p><dt><tt>int sscanf(const char *s, const char *format, ...)</tt><dd><tt>sscanf(s, ...)</tt> is equivalent to <tt>scanf(...)</tt> except thatthe input characters are taken from the string <tt>s</tt>.</dl><h3><a name="sb.1.4">B.1.4 Character Input and Output Functions</a></h3><dl><p><dt><tt>int fgetc(FILE *stream)</tt><dd><tt>fgetc</tt> returns the next character of <tt>stream</tt> as an<tt>unsigned char</tt> (converted to an <tt>int</tt>), or <tt>EOF</tt> ifend of file or error occurs.<p><dt><tt>char *fgets(char *s, int n, FILE *stream)</tt><dd><tt>fgets</tt> reads at most the next <tt>n-1</tt> characters into thearray <tt>s</tt>, stopping if a newline is encountered; the newline isincluded in the array, which is terminated by <tt>'\0'</tt>.<tt>fgets</tt> returns <tt>s</tt>, or <tt>NULL</tt> if end of file orerror occurs.<p><dt><tt>int fputc(int c, FILE *stream)</tt><dd><tt>fputc</tt> writes the character <tt>c</tt> (converted to an<tt>unsigend char</tt>) on <tt>stream</tt>. It returns the characterwritten, or <tt>EOF</tt> for error.<p><dt><tt>int fputs(const char *s, FILE *stream)</tt><dd><tt>fputs</tt> writes the string <tt>s</tt> (which need not contain<tt>\n</tt>) on <tt>stream</tt>; it returns non-negative, or <tt>EOF</tt>for an error.<p><dt><tt>int getc(FILE *stream)</tt><dd><tt>getc</tt> is equivalent to <tt>fgetc</tt> except that if it is amacro, it may evaluate <tt>stream</tt> more than once.<p><dt><tt>int getchar(void)</tt><dd><tt>getchar</tt> is equivalent to <tt>getc(stdin)</tt>.<p><dt><tt>char *gets(char *s)</tt><dd><tt>gets</tt> reads the next input line into the array <tt>s</tt>; itreplaces the terminating newline with <tt>'\0'</tt>. It returns<tt>s</tt>, or <tt>NULL</tt> if end of file or error occurs.<p><dt><tt>int putc(int c, FILE *stream)</tt><dd><tt>putc</tt> is equivalent to <tt>fputc</tt> except that if it is a macro, it mayevaluate <tt>stream</tt> more than once.<p><dt><tt>int putchar(int c)</tt><dd><tt>putchar(c)</tt> is equivalent to <tt>putc(c,stdout)</tt>.<p><dt><tt>int puts(const char *s)</tt><dd><tt>puts</tt> writes the string <tt>s</tt> and a newline to<tt>stdout</tt>. It returns <tt>EOF</tt> if an error occurs, non-negativeotherwise.<p><dt><tt>int ungetc(int c, FILE *stream)</tt><dd><tt>ungetc</tt> pushes <tt>c</tt> (converted to an <tt>unsignedchar</tt>) back onto <tt>stream</tt>, where it will be returned on thenext read. Only one character of pushback per stream is guaranteed.<tt>EOF</tt> may not be pushed back. <tt>ungetc</tt> returns thecharacter pushed back, or <tt>EOF</tt> for error.</dl><h3><a name="sb.1.5">B.1.5 Direct Input and Output Functions</a></h3><dl><p><dt><tt>size_t fread(void *ptr, size_t size, size_t nobj, FILE *stream)</tt><dd><tt>fread</tt> reads from <tt>stream</tt> into the array <tt>ptr</tt> atmost <tt>nobj</tt> objects of size <tt>size</tt>. <tt>fread</tt> returnsthe number of objects read; this may be less than the number requested.<tt>feof</tt> and <tt>ferror</tt> must be used to determine status.<p><dt><tt>size_t fwrite(const void *ptr, size_t size, size_t nobj, FILE *stream)</tt><dd><tt>fwrite</tt> writes, from the array <tt>ptr</tt>, <tt>nobj</tt>objects of size <tt>size</tt> on <tt>stream</tt>. It returns the numberof objects written, which is less than <tt>nobj</tt> on error.</dl><h3><a name="sb.1.6">B.1.6 File Positioning Functions</a></h3><dl><p><dt><tt>int fseek(FILE *stream, long offset, int origin)</tt><dd><tt>fseek</tt> sets the file position for <tt>stream</tt>; a subsequentread or write will access data beginning at the new position. For abinary file, the position is set to <tt>offset</tt> characters from<tt>origin</tt>, which may be <tt>SEEK_SET</tt> (beginning),<tt>SEEK_CUR</tt> (current position), or <tt>SEEK_END</tt> (end of file).For a text stream, <tt>offset</tt> must be zero, or a value returned by<tt>ftell</tt> (in which case <tt>origin</tt> must be <tt>SEEK_SET</tt>).<tt>fseek</tt> returns non-zero on error.<p><dt><tt>long ftell(FILE *stream)</tt><dd><tt>ftell</tt> returns the current file position for <tt>stream</tt>, or<tt>-1</tt> on error.<p><dt><tt>void rewind(FILE *stream)</tt><dd><tt>rewind(fp)</tt> is equivalent to <tt>fseek(fp, 0L, SEEK_SET); clearerr(fp)</tt>.<p><dt><tt>int fgetpos(FILE *stream, fpos_t *ptr)</tt><dd><tt>fgetpos</tt> records the current position in <tt>stream</tt> in<tt>*ptr</tt>, for subsequent use by <tt>fsetpos</tt>. The type<tt>fpos_t</tt> is suitable for recording such values. <tt>fgetpos</tt>returns non-zero on error.<p><dt><tt>int fsetpos(FILE *stream, const fpos_t *ptr)</tt><dd><tt>fsetpos</tt> positions <tt>stream</tt> at the position recorded by<tt>fgetpos</tt> in <tt>*ptr</tt>. <tt>fsetpos</tt> returns non-zero onerror.</dl><h3><a name="sb.1.7">B.1.7 Error Functions</a></h3>Many of the functions in the library set status indicators when error or endof file occur. These indicators may be set and tested explicitly. Inaddition, the integer expression <tt>errno</tt> (declared in<tt>&lt;errno.h&gt;</tt>) may contain an error number that gives furtherinformation about the most recent error.<dl><p><dt><tt>void clearerr(FILE *stream)</tt><dd><tt>clearerr</tt> clears the end of file and error indicators for <tt>stream</tt>.<p><dt><tt>int feof(FILE *stream)</tt><dd><tt>feof</tt> returns non-zero if the end of file indicator for<tt>stream</tt> is set.<p><dt><tt>int ferror(FILE *stream)</tt><dd><tt>ferror</tt> returns non-zero if the error indicator for <tt>stream</tt> is set.<p><dt><tt>void perror(const char *s)</tt><dd><tt>perror(s)</tt> prints <tt>s</tt> and an implementation-defined error messagecorresponding to the integer in <tt>errno</tt>, as if by<p>&nbsp;&nbsp;&nbsp;&nbsp;<tt>fprintf(stderr, "%s: %s\n", s, "</tt><em>error message</em><tt>");</tt><p><dd>See <tt>strerror</tt> in <a href="appb.html#sb.3">Section B.3</a>.</dl><h2><a name="sb.2">B.2 Character Class Tests: &lt;ctype.h&gt;</a></h2>The header <tt>&lt;ctype.h&gt;</tt> declares functions for testingcharacters. For each function, the argument list is an <tt>int</tt>, whosevalue must be <tt>EOF</tt> or representable as an <tt>unsigned char</tt>, andthe return value is an <tt>int</tt>. The functions return non-zero (true) ifthe argument <tt>c</tt> satisfies the condition described, and zero if not.<p align="center"><table border=0><td><tt>isalnum(c)</tt></td><td><tt>isalpha(c)</tt> or <tt>isdigit(c)</tt> is true</td><tr><td><tt>isalpha(c)</tt></td><td><tt>isupper(c)</tt> or <tt>islower(c)</tt> is true</td><tr><td><tt>iscntrl(c)</tt></td><td>control character</td><tr><td><tt>isdigit(c)</tt></td><td>decimal digit</td><tr><td><tt>isgraph(c)</tt></td><td>printing character except space</td><tr><td><tt>islower(c)</tt></td><td>lower-case letter</td><tr><td><tt>isprint(c)</tt></td><td>printing character including space</td><tr><td><tt>ispunct(c)</tt></td><td>printing character except space or letter or digit</td><tr><td><tt>isspace(c)</tt></td><td>space, formfeed, newline, carriage return, tab, vertical tab</td><tr><td><tt>isupper(c)</tt></td><td>upper-case letter</td><tr><td><tt>isxdigit(c)</tt></td><td>hexadecimal digit</td></table><p>In the seven-bit ASCII character set, the printing characters are<tt>0x20 (' ')</tt> to <tt>0x7E ('-')</tt>; the control characters are 0<tt>NUL</tt> to <tt>0x1F</tt> (US), and <tt>0x7F</tt> (DEL).<p>In addition, there are two functions that convert the case of letters:<p align="center"><table border=0><td><tt>int tolower(c)</tt></td><td>convert <tt>c</tt> to lower case</td><tr><td><tt>int toupper(c)</tt></td><td>convert <tt>c</tt> to upper case</td></table><p>If <tt>c</tt> is an upper-case letter, <tt>tolower(c)</tt> returns thecorresponding lower-case letter, <tt>toupper(c)</tt> returns thecorresponding upper-case letter; otherwise it returns <tt>c</tt>.<h2><a name="sb.3">B.3 String Functions: &lt;string.h&gt;</a></h2>There are two groups of string functions defined in the header<tt>&lt;string.h&gt;</tt>. The first have names beginning with <tt>str</tt>;the second have names beginning with <tt>mem</tt>. Except for<tt>memmove</tt>, the behavior is undefined if copying takes place betweenoverlapping objects. Comparison functions treat arguments as <tt>unsignedchar</tt> arrays.<p>In the following table, variables <tt>s</tt> and <tt>t</tt> are of type<tt>char *</tt>; <tt>cs</tt> and <tt>ct</tt> are of type <tt>const char*</tt>; <tt>n</tt> is of type <tt>size_t</tt>; and <tt>c</tt> is an<tt>int</tt> converted to <tt>char</tt>.<p align="center"><table border=0><td><tt>char *strcpy(s,ct)</tt></td><td>copy string <tt>ct</tt> to string <tt>s</tt>, including                                        <tt>'\0'</tt>; return <tt>s</tt>.</td><tr><td><tt>char *strncpy(s,ct,n)</tt></td><td>copy at most <tt>n</tt> characters of string <tt>ct</tt>                                           to <tt>s</tt>; return <tt>s</tt>. Pad with <tt>'\0'</tt>'s                                           if <tt>ct</tt> has fewer than <tt>n</tt> characters.</td><tr><td><tt>char *strcat(s,ct)</tt></td><td>concatenate string <tt>ct</tt> to end of string                                           <tt>s</tt>; return <tt>s</tt>.</td><tr>

⌨️ 快捷键说明

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