Skip to content

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.

Your browser cannot display this PDF. Download the PDF.

PDF Download Link

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.

SOLUTION
1
2
3
4
5
#include <stdio.h>

int main() {
    printf("hello world\n");
}
OUTPUT
hello world


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.

SOLUTION
1
2
3
4
5
#include <stdio.h>

int main() {
    printf("test : \c,\q,\y,\z,\w,\!,\@,\# : test");
}
OUTPUT
test : c,q,y,z,w,!,@,# : test


1.2 Variables and Arithmetic Expressions

Original temperature conversion program
#include <stdio.h>
/* print Fahrenheit-Celsius table
for fahr = 0, 20, ..., 300; floating-point version */
int main() {
    float fahr, celsius;
    float 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.0 / 9.0) * (fahr - 32.0);
        printf("%3.0f %6.1f\n", fahr, celsius);
        fahr = fahr + step;
    }
}

Exercise 1-3

Modify the temperature conversion program to print a heading above the table.

SOLUTION
#include <stdio.h>
/* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300; floating-point version */
int main() {
    float fahr, celsius;
    float lower, upper, step;
    lower = 0;   /* lower limit of temperature scale */
    upper = 300; /* upper limit */
    step = 20;   /* step size */
    fahr = lower;
    printf("%3s %6s\n", "F", "C");
    while(fahr <= upper) {
        celsius = (5.0 / 9.0) * (fahr - 32.0);
        printf("%3.0f %6.1f\n", fahr, celsius);
        fahr = fahr + step;
    }
}
OUTPUT
  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)) + 32 to convert from Fahrenheit to Celsius.

SOLUTION
#include <stdio.h>
/* Prints common temperatures from Celsius to Fahrenheit */
int main() {
    float fahr, celsius;
    float lower, upper, step;
    lower = 0;   /* lower limit of temperature scale */
    upper = 300; /* upper limit */
    step = 20;   /* step size */
    celsius = lower;
    printf("%3s %6s\n", "C", "F");
    while(celsius <= upper) {
        fahr = (celsius * (9.0 / 5.0)) + 32;
        printf("%3.0f %6.1f\n", celsius, fahr);
        celsius = celsius + step;
    }
}
OUTPUT
  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