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

📄 chapter7.html

📁 Kernighan and Ritchie - The C Programming Language c程序设计语言(第二版)称作是C语言学习的圣经
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<html><head><title>Chapter 7 - Input and Output</title></head><body><hr><p align="center"><a href="chapter6.html">Back to Chapter 6</a>&nbsp;--&nbsp;<a href="kandr.html">Index</a>&nbsp;--&nbsp;<a href="chapter8.html">Chapter 8</a><p><hr><h1>Chapter 7 - Input and Output</h1>Input and output are not part of the C language itself, so we have notemphasized them in our presentation thus far. Nonetheless, programs interactwith their environment in much more complicated ways than those we have shownbefore. In this chapter we will describe the standard library, a set offunctions that provide input and output, string handling, storage management,mathematical routines, and a variety of other services for C programs. Wewill concentrate on input and output<p>The ANSI standard defines these library functions precisely, so that they canexist in compatible form on any system where C exists. Programs that confinetheir system interactions to facilities provided by the standard library canbe moved from one system to another without change.<p>The properties of library functions are specified in more than a dozenheaders; we have already seen several of these, including<tt>&lt;stdio.h&gt;</tt>, <tt>&lt;string.h&gt;</tt>, and<tt>&lt;ctype.h&gt;</tt>. We will not present the entire library here, sincewe are more interested in writing C programs that use it. The library isdescribed in detail in <a href="appb.html">Appendix B</a>.<h2><a name="s7.1">7.1 Standard Input and Output</a></h2>As we said in <a href="chapter1.html">Chapter 1</a>, the library implements a simple modelof text input and output. A text stream consists of a sequence of lines; eachline ends with a newline character. If the system doesn't operate that way,the library does whatever necessary to make it appear as if it does. Forinstance, the library might convert carriage return and linefeed to newlineon input and back again on output.<p>The simplest input mechanism is to read one character at a time from the<em>standard input</em>, normally the keyboard, with <tt>getchar</tt>:<pre>   int getchar(void)</pre><tt>getchar</tt> returns the next input character each time it is called,or <tt>EOF</tt> when it encounters end of file. The symbolic constant<tt>EOF</tt> is defined in <tt>&lt;stdio.h&gt;</tt>. The value is typically-1, bus tests should be written in terms of <tt>EOF</tt> so as to beindependent of the specific value.<p>In many environments, a file may be substituted for the keyboard by using the&lt; convention for input redirection: if a program <tt>prog</tt> uses<tt>getchar</tt>, then the command line<pre>   prog &lt;infile</pre>causes <tt>prog</tt> to read characters from <tt>infile</tt> instead. Theswitching of the input is done in such a way that <tt>prog</tt> itself isoblivious to the change; in particular, the string ``<tt>&lt;infile</tt>'' isnot included in the command-line arguments in <tt>argv</tt>. Input switchingis also invisible if the input comes from another program via a pipemechanism: on some systems, the command line<pre>   otherprog | prog</pre>runs the two programs <tt>otherprog</tt> and <tt>prog</tt>, and pipes thestandard output of <tt>otherprog</tt> into the standard input for <tt>prog</tt>.<p>The function<pre>   int putchar(int)</pre>is used for output: <tt>putchar(c)</tt> puts the character <tt>c</tt> on the<tt>standard output</tt>, which is by default the screen. <tt>putchar</tt>returns the character written, or <tt>EOF</tt> is an error occurs. Again,output can usually be directed to a file with &gt;<em>filename</em>: if<tt>prog</tt> uses <tt>putchar</tt>,<pre>   prog &gt;outfile</pre>will write the standard output to <tt>outfile</tt> instead. If pipes aresupported,<pre>   prog | anotherprog</pre>puts the standard output of <tt>prog</tt> into the standard input of<tt>anotherprog</tt>.<p>Output produced by <tt>printf</tt> also finds its way to the standard output.Calls to <tt>putchar</tt> and <tt>printf</tt> may be interleaved - outputhappens in the order in which the calls are made.<p>Each source file that refers to an input/output library function must containthe line<pre>   #include &lt;stdio.h&gt;</pre>before the first reference. When the name is bracketed by &lt; and &gt; asearch is made for the header in a standard set of places (for example, onUNIX systems, typically in the directory <tt>/usr/include</tt>).<p>Many programs read only one input stream and write only one output stream;for such programs, input and output with <tt>getchar</tt>, <tt>putchar</tt>,and <tt>printf</tt> may be entirely adequate, and is certainly enough to getstarted. This is particularly true if redirection is used to connect theoutput of one program to the input of the next. For example, consider theprogram <tt>lower</tt>, which converts its input to lower case:<pre>   #include &lt;stdio.h&gt;   #include &lt;ctype.h&gt;   main() /* lower: convert input to lower case*/   {       int c       while ((c = getchar()) != EOF)           putchar(tolower(c));       return 0;   }</pre>The function <tt>tolower</tt> is defined in <tt>&lt;ctype.h&gt;</tt>; itconverts an upper case letter to lower case, and returns other charactersuntouched. As we mentioned earlier, ``functions'' like <tt>getchar</tt> and<tt>putchar</tt> in <tt>&lt;stdio.h&gt;</tt> and <tt>tolower</tt> in<tt>&lt;ctype.h&gt;</tt> are often macros, thus avoiding the overhead of afunction call per character. We will show how this is done in<a href="chapter8.html#s8.5">Section 8.5</a>. Regardless of how the<tt>&lt;ctype.h&gt;</tt> functions are implemented on a given machine,programs that use them are shielded from knowledge of the character set.<p><strong>Exercise 7-1.</strong> Write a program that converts upper case tolower or lower case to upper, depending on the name it is invoked with, asfound in <tt>argv[0]</tt>.<h2><a name="s7.2">7.2 Formatted Output - printf</a></h2>The output function <tt>printf</tt> translates internal values to characters.We have used <tt>printf</tt> informally in previous chapters. The descriptionhere covers most typical uses but is not complete; for the full story, see<a href="appb.html">Appendix B</a>.<pre>   int printf(char *format, arg1, arg2, ...);</pre><tt>printf</tt> converts, formats, and prints its arguments on the standardoutput under control of the <tt>format</tt>. It returns the number ofcharacters printed.<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>printf</tt>.Each conversion specification begins with a % and ends with a conversioncharacter. Between the % and the conversion character there may be, in order:<ul><li>A minus sign, which specifies left adjustment of the converted argument.<li>A number that specifies the minimum field width. The converted argument    will be printed in a field at least this wide. If necessary it will be    padded on the left (or right, if left adjustment is called for) to make    up the field width.<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 after the decimal    point of a floating-point value, or the minimum number of digits for an    integer.<li>An <tt>h</tt> if the integer is to be printed as a <tt>short</tt>, or    <tt>l</tt> (letter ell) if as a <tt>long</tt>.</ul>Conversion characters are shown in Table 7.1. If the characterafter the % is not a conversion specification, the behavior is undefined.<p align="center"><em><strong>Table 7.1</strong> Basic Printf Conversions</em><p><table align="center" border=1><th align="center">Character<th align="center">Argument type; Printed As<tr><td><tt>d,i</tt></td><td><tt>int</tt>; decimal number</td><tr><td><tt>o</tt>  </td><td><tt>int</tt>; unsigned octal number (without a                         leading zero)</td><tr><td><tt>x,X</tt></td><td><tt>int</tt>; unsigned hexadecimal number (without a                         leading <tt>0x</tt> or <tt>0X</tt>), using                         <tt>abcdef</tt> or <tt>ABCDEF</tt> for 10, ...,15.</td><tr><td><tt>u</tt>  </td><td><tt>int</tt>; unsigned decimal number</td><tr><td><tt>c</tt>  </td><td><tt>int</tt>; single character</td><tr><td><tt>s</tt></td><td><tt>char *</tt>; print characters from the string                       until a <tt>'\0'</tt> or the number of characters                       given by the precision.</td><tr><td><tt>f</tt></td><td><tt>double</tt>; <tt>[-]</tt><em>m.dddddd</em>, where                       the number of <em>d</em>'s is given by the precision                       (default 6).</td><tr><td><tt>e,E</tt></td><td><tt>double</tt>;                         <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 given by the                         precision (default 6).</td><tr><td><tt>g,G</tt></td><td><tt>double</tt>; use <tt>%e</tt> or <tt>%E</tt> if                         the exponent is less than -4 or greater than or                         equal to the precision; otherwise use <tt>%f</tt>.                         Trailing zeros and a trailing decimal point are not                         printed.</td><tr><td><tt>p</tt>  </td><td><tt>void *</tt>; pointer (implementation-dependent                         representation).</td><tr><td><tt>%</tt> </td><td>no argument is converted; print a %</td></table><p>A width or precision may be specified as *, in which case the value iscomputed by converting the next argument (which must be an <tt>int</tt>). Forexample, to print at most <tt>max</tt> characters from a string <tt>s</tt>,<pre>   printf("%.*s", max, s);</pre>Most of the format conversions have been illustrated in earlier chapters. Oneexception is the precision as it relates to strings. The following tableshows the effect of a variety of specifications in printing ``hello, world''(12 characters). We have put colons around each field so you can see itextent.<pre>   :%s:          :hello, world:   :%10s:        :hello, world:   :%.10s:       :hello, wor:   :%-10s:       :hello, world:   :%.15s:       :hello, world:   :%-15s:       :hello, world   :   :%15.10s:     :     hello, wor:   :%-15.10s:    :hello, wor     :</pre>A warning: <tt>printf</tt> uses its first argument to decide how manyarguments follow and what their type is. It will get confused, and you willget wrong answers, if there are not enough arguments of if they are the wrongtype. You should also be aware of the difference between these two calls:<pre>   printf(s);         /* FAILS if s contains % */   printf("%s", s);   /* SAFE */</pre>The function <tt>sprintf</tt> does the same conversions as <tt>printf</tt>does, but stores the output in a string:<pre>   int sprintf(char *string, char *format, arg1, arg2, ...);</pre><tt>sprintf</tt> formats the arguments in <tt>arg1</tt>, <tt>arg2</tt>, etc.,according to <tt>format</tt> as before, but places the result in<tt>string</tt> instead of the standard output; <tt>string</tt> must be bigenough to receive the result.<p><strong>Exercise 7-2.</strong> Write a program that will print arbitraryinput in a sensible way. As a minimum, it should print non-graphic charactersin octal or hexadecimal according to local custom, and break long text lines.<h2><a name="s7.3">7.3 Variable-length Argument Lists</a></h2>This section contains an implementation of a minimal version of<tt>printf</tt>, to show how to write a function that processes avariable-length argument list in a portable way. Since we are mainlyinterested in the argument processing, <tt>minprintf</tt> will process theformat string and arguments but will call the real <tt>printf</tt> to do theformat conversions.<p>The proper declaration for <tt>printf</tt> is<pre>   int printf(char *fmt, ...)</pre>where the declaration <tt>...</tt> means that the number and types of thesearguments may vary. The declaration <tt>...</tt> can only appear at the endof an argument list. Our <tt>minprintf</tt> is declared as<pre>   void minprintf(char *fmt, ...)</pre>since we will not return the character count that <tt>printf</tt> does.<p>The tricky bit is how <tt>minprintf</tt> walks along the argument list whenthe list doesn't even have a name. The standard header<tt>&lt;stdarg.h&gt;</tt> contains a set of macro definitions that define howto step through an argument list. The implementation of this header will varyfrom machine to machine, but the interface it presents is uniform.<p>The type <tt>va_list</tt> is used to declare a variable that will refer toeach argument in turn; in <tt>minprintf</tt>, this variable is called<tt>ap</tt>, for ``argument pointer.'' The macro <tt>va_start</tt>initializes <tt>ap</tt> to point to the first unnamed argument. It must becalled once before <tt>ap</tt> is used. There must be at least one namedargument; the final named argument is used by <tt>va_start</tt> to getstarted.<p>Each call of <tt>va_arg</tt> returns one argument and steps <tt>ap</tt> tothe next; <tt>va_arg</tt> uses a type name to determine what type to returnand how big a step to take. Finally, <tt>va_end</tt> does whatever cleanup isnecessary. It must be called before the program returns.<p>These properties form the basis of our simplified <tt>printf</tt>:<pre>   #include &lt;stdarg.h&gt;   /* minprintf: minimal printf with variable argument list */   void minprintf(char *fmt, ...)   {       va_list ap; /* points to each unnamed arg in turn */       char *p, *sval;       int ival;       double dval;       va_start(ap, fmt); /* make ap point to 1st unnamed arg */       for (p = fmt; *p; p++) {           if (*p != '%') {               putchar(*p);               continue;           }           switch (*++p) {           case 'd':               ival = va_arg(ap, int);               printf("%d", ival);               break;           case 'f':               dval = va_arg(ap, double);               printf("%f", dval);               break;           case 's':               for (sval = va_arg(ap, char *); *sval; sval++)                   putchar(*sval);               break;           default:               putchar(*p);               break;           }       }       va_end(ap); /* clean up when done */   }</pre><strong>Exercise 7-3.</strong> Revise <tt>minprintf</tt> to handle more ofthe other facilities of <tt>printf</tt>.

⌨️ 快捷键说明

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