5 <style|<tuple|book|style-bk>>
10 <section|Ch. 1: A Tutorial Introduction>
12 <subsection|Sec. 1.1: Getting Started>
17 <item*|Exercise 1-1>Run the \P<verbatim|hello, world>\Q program on your
18 system. Experiment with leaving out parts of the program, to see what
19 error messages you get.
21 <item*|Exercise 1-2>Experiment to find out what happens when
22 <cpp|printf>'s argument string contains <cpp|\\><em|c>, where <em|c> is
23 some character not listed above.
26 <subsection|Sec. 1.2: Variables and Arithmetic Expressions>
29 <item*|Exercise 1-3>Modify the temperature conversion program to bring a
30 heading above the table.
32 <item*|Exercise 1-4>Write a program to bring the corresponding Celsius to
36 <subsection|Sec. 1.3: The For Statement>
39 <item*|Exercise 1-5>Modify the temperature conversion program to print
40 the table in reverse order, that is, from <math|300> degrees to <math|0>.
43 <subsection|Sec. 1.4: Symbolic Constants>
45 <subsection|Sec. 1.5: Character Input and Output>
47 <subsubsection|Sec. 1.5.1: File Copying>
50 <item*|Exercise 1-6>Verify that the expression <cpp|getchar() != EOF> is
53 <item*|Exercise 1-7>Write a program to print the value of <cpp|EOF>.
56 <subsubsection|Sec. 1.5.2: Character Counting>
58 <subsubsection|Sec. 1.5.3: Line Counting>
61 <item*|Exercise 1-8>Write a program to count blanks, tabs, and newlines.
63 <item*|Exercise 1-9>Write a program to copy its input to its output,
64 replacing each string of one or more blanks by a single blank.
66 <item*|Exercise 1-10>Write a program to copy its input to its output,
67 replacing each tab by <cpp|\\t>, each backspace by <cpp|\\b>, and each
68 backslash by <cpp|\\\\>. This makes tabs and backspaces visible in an
72 <subsubsection|Sec. 1.5.4: Word Counting>
75 <item*|Exercise 1-11>How would you test the word program? What kinds of
76 input are most likely to uncover bugs if there are any?
78 <item*|Exercise 1-12>Write a program that prints its input one word per
82 <subsection|Sec. 1.6: Arrays>
85 <item*|Exercise 1-13>Write a program to print a histogram of the lengths
86 of words in its input. IT is easy to draw the histogram with the bars
87 horizontal; a vertical orientation is more challenging.
89 <item*|Exercise 1-14>Write a program to print a histogram of the
90 frequencies of different characters in its input.
93 <subsection|Sec 1.7: Functions>
96 <item*|Exercise 1-15>Rewrite the temperature conversion program of
97 Section 1.2 to use a function for conversion.
100 <subsection|Sec 1.8: Arguments\UCall by Value>
102 <subsection|Sec 1.9: Character Arrays>
105 <item*|Exercise 1-16>Revise the main routine of the longest-line program
106 so it will correctly print the length of arbitrarily long input lines,
107 and as much as possible of the text.
109 <item*|Exercise 1-17>Write a program to print all input lines that are
110 longer than <math|80> characters.
112 <item*|Exercise 1-18>Write a program to remove trailing blanks and tabs
113 from each line of input, and to delete entirely blank lines.
115 <item*|Exercise 1-19>Write a function <cpp|reverse(s)> that reverses the
116 character string <cpp|s>. Use it to write a program that reverses its
117 input a line at a time.
120 <subsection|Sec 1.10: External Variables and Scope>
123 <item*|Exercise 1-20>Write a program <cpp|detab> that replaces tabs in
124 the input with the proper number of blanks to space to the next tab stop.
125 Assume a fixed set of tab stops, say every <em|n> columns. Should <em|n>
126 be a variable or a symbolic parameter?
128 <item*|Exercise 1-21>Write a program <cpp|entab> that replaces strings of
129 blanks by the minimum number of tabs and blanks to achieve the same
130 spacing. Use the same tab stops as for <cpp|detab>. When either a tab or
131 a single blank would suffice to each a tab stop, which should be given
134 <item*|Exercise 1-22>Write a program to \Pfold\Q long input lines into
135 two or more shorter lines after the last non-blank character that occurs
136 before the <em|n>-th column of input. Make sure your program does
137 something intelligent with very long lines, and if there are no blanks or
138 tabs before the specified column.
140 <item*|Exercise 1-23>Write a program to remove all comments from a
141 <name|C> program. Don't forget to handle quoted strings and character
142 constants properly. <name|C> comments do not nest.
144 <item*|Exercise 1-24>Write a program to check a <name|C> program for
145 rudimentary syntax errors like unbalanced parentheses, brackets and
146 braces. Don't forget about quotes, both single and double, escape
147 sequences, and comments. (This program is hard if you do it in full
151 <section|Ch2: Types, Operators, and Expressions>
153 <subsection|Sec 2.1: Variable names>
155 <subsection|Sec 2.2: Data Types and Sizes>
158 <item*|Exercise 2-1>Write a program to determine the ranges of
159 <cpp|char>, <cpp|short>, <cpp|int>, and <cpp|long> variables, both
160 <cpp|signed> and <cpp|unsigned>, by printing appropriate values from
161 standard headers and by direct computation. Harder if you compute them:
162 determine the ranges of the various floating-point types.
165 <subsection|Sec 2.3: Constants>
167 <subsection|Sec 2.4: Declarations>
169 <subsection|Sec 2.5: Arithmetic Operators>
171 <subsection|Sec 2.6: Relational and Logical Operators>
174 <item*|Exercise 2-2>Write a loop equivalent to the <cpp|for> loop above
175 without using <cpp|&&> or <cpp|\|\|>.
178 <subsection|Sec 2.7: Type Conversions>
181 <item*|Exercise 2-3>Write the function <cpp|htoi(s)>, which converts a
182 string of hexadecimal digits (including an optional <cpp|0x> or <cpp|0X>)
183 into its equivalent integer value. The allowable digits are <cpp|0>
184 through <cpp|9>, <cpp|a> through <cpp|f>, and <cpp|A> through <cpp|F>.
187 <subsection|Sec 2.8: Increment and Decrement Operators>
190 <item*|Exercise 2-4>Write an alternate version of <cpp|squeeze(s1,s2)>
191 that deletes each character in <cpp|s1> that matches any character in the
192 <em|string> <cpp|s2>.
194 <item*|Exercise 2-5>Write the function <cpp|any(s1,s2)>, which returns
195 the first location in the string <cpp|s1> where any character fromt he
196 string <cpp|s2> occurs, or <cpp|-1> if <cpp|s1> contains no characters
197 from <cpp|s2>. (The standard library function <cpp|strpbrk> does the same
198 job but returns a pointer to the location.)
201 <subsection|Sec 2.9: Bitwise Operators>
204 <item*|Exercise 2-6>Write a function <cpp|setbits(x,p,n,y)> that returns
205 <cpp|x> with the <cpp|n> bits that begin at position <cpp|p> set to the
206 rightmost <cpp|n> bits of <cpp|y>, leaving the other bits unchanged.
208 <item*|Exercise 2-7>Write a function <cpp|invert(x,p,n)> that returns
209 <cpp|x> with the <cpp|n> bits that begin at position <cpp|p> inverted
210 (i.e. <math|1> changed into <math|0> and vice versa), leaving the others
213 <item*|Exercise 2-8>Write a function <cpp|rightrot(x,n)> that returns the
214 value of the integer <cpp|x> rotated to the right by <cpp|n> bit
218 <subsection|Sec 2.10: Assignment Operators and Expressions>
221 <item*|Exercise 2-9>In a two\<#2019\>s complement number system, <cpp|x
222 &= (x-1)> deletes the rightmost <math|1>-bit in <cpp|x>. Explain why. Use
223 this observation to write a faster version of <cpp|bitcount>.
226 <subsection|Sec 2.11: Conditional Expressions>
229 <item*|Exercise 2-10>Rewrite the function <cpp|lower>, which converts
230 upper case letters to lower case, with a conditional expression instead
234 <subsection|Sec 2.12: Precedence and Order of Evaluation>
243 <associate|page-medium|papyrus>
249 <associate|auto-1|<tuple|1|1|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
250 <associate|auto-10|<tuple|1.1.5.3|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
251 <associate|auto-11|<tuple|1.1.5.4|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
252 <associate|auto-12|<tuple|1.1.6|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
253 <associate|auto-13|<tuple|1.1.7|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
254 <associate|auto-14|<tuple|1.1.8|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
255 <associate|auto-15|<tuple|1.1.9|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
256 <associate|auto-16|<tuple|1.1.10|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
257 <associate|auto-17|<tuple|1.2|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
258 <associate|auto-18|<tuple|1.2.1|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
259 <associate|auto-19|<tuple|1.2.2|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
260 <associate|auto-2|<tuple|1.1|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
261 <associate|auto-20|<tuple|1.2.3|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
262 <associate|auto-21|<tuple|1.2.4|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
263 <associate|auto-22|<tuple|1.2.5|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
264 <associate|auto-23|<tuple|1.2.6|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
265 <associate|auto-24|<tuple|1.2.7|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
266 <associate|auto-25|<tuple|1.2.8|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
267 <associate|auto-26|<tuple|1.2.9|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
268 <associate|auto-27|<tuple|1.2.10|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
269 <associate|auto-28|<tuple|1.2.11|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
270 <associate|auto-29|<tuple|1.2.12|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
271 <associate|auto-3|<tuple|1.1.1|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
272 <associate|auto-30|<tuple|1.2.13|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
273 <associate|auto-4|<tuple|1.1.2|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
274 <associate|auto-5|<tuple|1.1.3|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
275 <associate|auto-6|<tuple|1.1.4|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
276 <associate|auto-7|<tuple|1.1.5|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
277 <associate|auto-8|<tuple|1.1.5.1|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
278 <associate|auto-9|<tuple|1.1.5.2|?|../../../../.TeXmacs/texts/scratch/no_name_10.tm>>
285 <vspace*|1fn><with|font-series|<quote|bold>|math-font-series|<quote|bold>|1<space|2spc>K&R
286 Exercises> <datoms|<macro|x|<repeat|<arg|x>|<with|font-series|medium|<with|font-size|1|<space|0.2fn>.<space|0.2fn>>>>>|<htab|5mm>>
287 <no-break><pageref|auto-1><vspace|0.5fn>