* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / notes.accompany.ansi.c / sx4f.html
blob5086960496fb7def6014c1e68ff9d158704df02c
1 <!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995, 1996 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
5 <html>
6 <head>
7 <link rev="owner" href="mailto:scs@eskimo.com">
8 <link rev="made" href="mailto:scs@eskimo.com">
9 <title>section 1.5.1: File Copying</title>
10 <link href="sx4e.html" rev=precedes>
11 <link href="sx4g.html" rel=precedes>
12 <link href="sx4.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>section 1.5.1: File Copying</H2>
17 page 16
19 <p>Pay particular attention to the discussion of why the variable
20 to hold <TT>getchar</TT>'s return value is declared as an
21 <TT>int</TT> rather than a <TT>char</TT>.
22 The distinction may not seem terribly significant
23 now,
24 but it is important.
25 If you use a <TT>char</TT>, it may seem to work,
26 but it may break down mysteriously later.
27 Always remember to use an <TT>int</TT> for anything you assign
28 <TT>getchar</TT>'s return value to.
29 </p><p>page 17
30 </p><p>The line
31 <pre> while ((c = getchar()) != EOF)
32 </pre>epitomizes the cryptic brevity which C is notorious for.
33 You may find this terseness infuriating
34 (and you're not alone!),
35 and it can certainly be carried too far,
36 but bear with me for a moment while I defend it.
37 </p><p>The simple example on pages 16 and 17 illustrates the tradeoffs
38 well.
39 We have four things to do:
40 <OL><li>call <TT>getchar</TT>,
41 <li>assign its return value to a variable,
42 <li>test the return value against EOF,
43 and
44 <li>process the character
45 (in this case, print it again).
46 </OL>We can't eliminate any of these steps.
47 We have to assign <TT>getchar</TT>'s value to a variable
48 (we can't just use it directly)
49 because we have to do two different things with it
50 (test, and print).
51 Therefore, compressing the assignment and test into the same line
52 (as on page 17)
53 is the only
55 good
56 way of avoiding two distinct calls to <TT>getchar</TT>
57 (as on page 16).
58 You may not agree that the compressed idiom is better for being
59 more compact or easier to read,
60 but the fact that there is now only one call to
61 <TT>getchar</TT> <em>is</em> a real virtue.
62 </p><p>In a tiny program like this,
63 the repeated call to <TT>getchar</TT> isn't much of a problem.
64 But in a real program,
65 if the thing being read is at all complicated
66 (not just a single character read with <TT>getchar</TT>),
67 and if the processing is at all complicated
68 (such that the input call before the loop and the input call
69 at the end of the loop become widely separated),
70 and if the way that input is done is ever changed some day,
71 it's just too likely that one of the input calls will get
72 changed but not the other.
73 </p><p>(Also,
74 note that when an assignment like
75 <TT>c = getchar()</TT> appears within a larger expression,
76 the surrounding expression receives the same value that is assigned.
77 Using an assignment as a subexpression in this way
78 is perfectly legal
79 and quite common
80 in C.)
81 </p><p>When you run the character copying program,
82 and it begins copying its input (your typing) to its output (your screen),
83 you may find yourself wondering how to stop it.
84 It stops when it receives end-of-file (EOF), but how do you send EOF?
85 The answer depends on what kind of computer you're using.
86 On Unix and Unix-related systems, it's almost always control-D.
87 On MS-DOS machines, it's control-Z followed by the RETURN key.
88 Under Think C on the Macintosh, it's control-D, just like Unix.
89 On other systems, you may have to do some research to learn how to send EOF.
90 </p><p>(Note, too, that
91 the character you type to generate an end-of-file condition from the keyboard
92 has nothing to do with the <TT>EOF</TT> value returned by <TT>getchar</TT>.
93 The <TT>EOF</TT> value returned by <TT>getchar</TT> is a code
94 indicating that the input system has detected an end-of-file condition,
95 whether it's reading the keyboard or a file
96 or a magnetic tape or a network connection or anything else.)
97 </p><p>Another excellent thing to know when doing any kind of programming
98 is how to terminate a runaway program.
99 If a program is running forever waiting for input,
100 you can usually stop it by sending it an end-of-file, as above,
101 but if it's running forever <em>not</em> waiting for something
102 (i.e. if it's in an infinite loop)
103 you'll have to take more drastic measures.
104 Under Unix, control-C will terminate the current program,
105 almost no matter what.
106 Under MS-DOS, control-C or control-BREAK will sometimes
107 terminate the current program,
108 but by default MS-DOS only checks for control-C when it's looking for input,
109 so an infinite loop can be unkillable.
110 There's a DOS command,
111 I think it's
112 <pre> break on
113 </pre>which tells DOS to look for control-C more often,
114 and I recommend using this command if you're doing any programming.
115 (If a program is in a really tight infinite loop under MS-DOS,
116 there can be no way of killing it short of rebooting.)
117 On the Mac, try command-period or command-option-ESCAPE.
118 </p><p>Finally, don't be disappointed
119 (as I was)
120 the first time you run
121 the character copying program.
122 You'll type a character, and see it on the screen right away,
123 and assume it's your program working,
124 but it's only your computer echoing every key you type,
125 as it always does.
126 When you hit RETURN,
127 a full line of characters is made available to your program,
128 which it reads all at once,
129 and then copies to the screen
130 (again).
131 In other words, when you run this program,
132 it will probably seem to echo the input a line at a time,
133 rather than a character at a time.
134 You may wonder how a program can read a character right away,
135 without waiting for the user to hit RETURN.
136 That's an excellent question,
137 but unfortunately the answer is rather complicated,
139 beyond the scope of this introduction.
140 (Among other things,
141 how to read a character right away is one of the things that's
142 not defined by the C language,
143 and it's not defined by any of the standard library functions,
144 either.
145 How to do it depends on which operating system you're using.)
146 </p><hr>
148 Read sequentially:
149 <a href="sx4e.html" rev=precedes>prev</a>
150 <a href="sx4g.html" rel=precedes>next</a>
151 <a href="sx4.html" rev=subdocument>up</a>
152 <a href="top.html">top</a>
153 </p>
155 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
156 // <a href="copyright.html">Copyright</a> 1995, 1996
157 // <a href="mailto:scs@eskimo.com">mail feedback</a>
158 </p>
159 </body>
160 </html>