Chapter 1 - A Tutorial Introduction

Ex
Pg
Exercise
Solution
Solved by
1
8
Run the "hello world" program on your system. Experiment with leaving out parts of the program, to see what error messages you get.

101

Richard Heathfield
2
8
Experiment to find out what happens when printf 's argument string contains \c, where c is some character not listed above.
Listing krx102
Richard Heathfield
3
13
Modify the temperature conversion program to print a heading above the table.
Listing krx103
Richard Heathfield
4
13
Write a program to print the corresponding Celsius to Fahrenheit table.
Listing krx104
Richard Heathfield
5
14
Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0.
Listing krx105
Category 0 Solutions by my good self [Richard Heathfield] and by Chris Sidi.
6
17
Verify that the expression getchar() != EOF is 0 or 1.
Listing krx106
Richard Heathfield
7
17
Write a program to print the value of EOF.
Listing krx107
Richard Heathfield
8
20
Write a program to count blanks, tabs, and newlines.
Listing krx108
Richard Heathfield
9
20
Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.
Listing krx109
Category 0 Solutions by my good self [Richard Heathfield], by Chris Sidi, and by Stig Brautaset.
10
20
Write a program to copy its input to its output, replacing each tab by \t , each backspace by \b , and each backslash by \\ . This makes tabs and backspaces visible in an unambiguous way.
Listing krx110
A couple of solutions here. One from me [Richard Heathfield], and one from Gregory Pietsch.
11
21
How would you test the word count program? What kinds of input are most likely to uncover bugs if there are any?
Listing krx111
Solution by Dann Corbit. Solution to Dann's follow-up challenge by Gregory Pietsch.
12
21
Write a program that prints its input one word per line.
Listing krx112
Richard Heathfield
13
24
Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging.
Listing krx113
Richard Heathfield
14
24
Write a program to print a histogram of the frequencies of different characters in its input.
Listing krx114
Richard Heathfield
15
27
Rewrite the temperature conversion program of Section 1.2 to use a function for conversion.
Listing krx115
Richard Heathfield
16
30
Revise the main routine of the longest-line program so it will correctly print the length of arbitrarily long input lines, and as much as possible of the text.
Listing krx116
"386sx" and myself [Richard Heathfield].
17
31
Write a program to print all input lines that are longer than 80 characters.
Listing krx117
"MJSR"
18
31
Write a program to remove all trailing blanks and tabs from each line of input, and to delete entirely blank lines.
Listing krx118
Solution by Ben Pfaff, modification by Chris Sidi
19
31
Write a function reverse(s) that reverses the character string s . Use it to write a program that reverses its input a line at a time.
Listing krx119
Richard Heathfield
20
34
Write a program detab that replaces tabs in the input with the proper number of blanks to space to the next tab stop. Assume a fixed set of tab stops, say every n columns. Should n be a variable or a symbolic parameter?
Listing krx120
Richard Heathfield
21
34
Write a program entab that replaces strings of blanks with the minimum number of tabs and blanks to achieve the same spacing. Use the same stops as for detab . When either a tab or a single blank would suffice to reach a tab stop, which should be given preference?
Listing krx121
Rick Dearman
22
34
Write a program to "fold" long input lines into two or more shorter lines after the last non-blank character that occurs before the n -th column of input. Make sure your program does something intelligent with very long lines, and if there are no blanks or tabs before the specified column.
Listing krx122
Category 1 Solution by Rick Dearman
23
34
Write a program to remove all comments from a C program. Don't forget to handle quoted strings and character constants properly. C comments do not nest.
Listing krx123
This was the first exercise for which solutions were submitted en masse from comp.lang.c, and as a result, quite a few solutions have been provided here. You may find it interesting to compare and contrast different approaches to the same problem.
24
34
Write a program to check a C program for rudimentary syntax errors like unbalanced parentheses, brackets and braces. Don't forget about quotes, both single and double, escape sequences, and comments. (This program is hard if you do it in full generality.)