I think we can all agree that K&R is the most hyped programming book of all time. Ive read the first 100 pages and I cant see what is so great about it, it just seems like any other programming book. But too many people I respect say that it is the best book for learning programming so I would like to go through the code in the book together with all you guys to try and see if I can discover what all these deep concepts everyone is talking about are.
So lets skip the hello world and go directly to the Fahrenheit-Celsius converter. The first version of this program is on page 12 and the last one which uses a for loop is on page 17. I will paste the first and last versions of this program and lets see what we can find:
pg12
#include <stdio.h>
/* print Fahrenheit-Celsius table
for fahr = 0, 20, ..., 300 */
main()
{
int fahr, celsius;
int lower, upper, step;
lower = 0; /* lower limit of temperature scale */
upper = 300; /* upper limit */
step = 20; /* step size */
fahr = lower;
while (fahr <= upper) {
celsius = 5 * (fahr-32) / 9;
printf("%d\t%d\n", fahr, celsius);
fahr = fahr + step;
}
}
pg17
#include <stdio.h>
#define LOWER 0 /* lower limit of table */
#define UPPER 300 /* upper limit */
#define STEP 20 /* step size */
/* print Fahrenheit-Celsius table */
main()
{
int fahr;
for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP)
printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
}
This just looks like a simple program to me, nothing special about it. Shall we go on to the file copying program on page 18 or am I missing anything?