The C Programming Language 2nd Edition Solutions¶
This page contains my solutions and notes for exercises from The C Programming Language, 2nd Edition by Brian W. Kernighan and Dennis M. Ritchie.
Chapter 1 - A Tutorial Introduction¶
1.1 Getting Started¶
Exercise 1-1¶
Run the "hello, world" program on your system. Experiment with leaving out parts of the program, to see what error messages you get.
Exercise 1-2¶
Experiment to find out what happens when printf's argument string contains \c, where c is some character not listed above.
NOTE: The following are example escape sequences that are not in the standard C, their behavior is undefined and may varry between compilers. In my case, it compiled with warnings and output the characters without the backslashes, while for another person it errored on their machine.
1.2 Variables and Arithmetic Expressions¶
Original temperature conversion program
Exercise 1-3¶
Modify the temperature conversion program to print a heading above the table.
F C
0 -17.8
20 -6.7
40 4.4
60 15.6
80 26.7
100 37.8
120 48.9
140 60.0
160 71.1
180 82.2
200 93.3
220 104.4
240 115.6
260 126.7
280 137.8
300 148.9
Exercise 1-4¶
Write a program to print the corresponding Celsius to Fahrenheit table.
NOTE: I used the formula
F = (C * (9 / 5)) + 32to convert from Fahrenheit to Celsius.
C F
0 32.0
20 68.0
40 104.0
60 140.0
80 176.0
100 212.0
120 248.0
140 284.0
160 320.0
180 356.0
200 392.0
220 428.0
240 464.0
260 500.0
280 536.0
300 572.0
1.3 The for statement¶
Exercise 1-5¶
Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0.
| SOLUTION | |
|---|---|
300 148.9
280 137.8
260 126.7
240 115.6
220 104.4
200 93.3
180 82.2
160 71.1
140 60.0
120 48.9
100 37.8
80 26.7
60 15.6
40 4.4
20 -6.7
0 -17.8