The C Programming Language notes

From Reboil

The two hardest problems in programming are cache invalidation, naming things and off-by-one errors.[1]


This page contains Baltakateiʼs notes for completing the exercises in The C Programming Language, 2nd Edition by Brian Kernighan and Dennis Ritchie.

Stats

Notes

Chapter 1

Ch1 Section 1

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.

#include <stdio.h>

int main()
{
  //  printf("Hello, world.\n");
}
Exercise 1-2

Experiment to find out what happens when printf's string contains \c, where c is some character not listed above.

Code:

#include <stdio.h>
int main()
{
  printf("hello\n");  
  printf("\v");        // Vertical tab
  printf("world\?\n"); // Question mark
  printf("foo\x0A");   // Newline (hexadecimal)
  printf("ba\br\n");   // Backspace
  printf("baz\b\n");   // Backspace moves cursor back but doesn't overwrite z
  printf("baz\b \n");  // Backspace overwrites z with space
}

Result:

hello

world?
foo
br
baz
ba 
Exercise 1-3

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

The temperature conversion program:

#include <stdio.h>
/* print Fahrenheit-Celsius table
  for fahr = 0, 20, ..., 300; floatinq-point version */
main()
{
  float fahr, celsius;
  int lower, upper, step;
  
  lower = 0;     /* lower limit of temperature table */
  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;
  }
}

Code:

#include <stdio.h>
/* print Fahrenheit-Celsius table
  for fahr = 0, 20, ..., 300; floatinq-point version */
int main()
{
  float fahr, celsius;
  int lower, upper, step;
  
  lower = 0;     /* lower limit of temperature table */
  upper = 300;   /* upper limit */
  step = 20;     /* step size */
  
  fahr = lower;
  printf("F\tC\n");
  while (fahr <= upper) {
    celsius = (5.0/9.0) * (fahr-32.0);
    printf("%4.0f\t%4.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
Exercise 1-5
Exercise 1-6

Verify that the expression c = getchar() != EOF (equivalent to c = ( getchar() != EOF )) sets the c variable to an integer with a value of either `0` or `1`.

Exercise 1-7

Print the EOF value specified by <stdio.h>.

Ch1 Section 2

Ch1 Section 3

Ch1 Section 4

Ch1 Section 5

Ch1 Section 6

Ch1 Section 7

Ch1 Section 8

Ch1 Section 9

Ch1 Section 10

Chapter 2

Ch2 Section 1

Ch2 Section 2

Ch2 Section 3

Ch2 Section 4

Ch2 Section 5

Ch2 Section 6

Ch2 Section 7

Ch2 Section 8

Ch2 Section 9

Ch2 Section 10

Ch2 Section 11

Ch2 Section 12

Chapter 3

Ch3 Section 1

Ch3 Section 2

Ch3 Section 3

Ch3 Section 4

Ch3 Section 5

Ch3 Section 6

Ch3 Section 7

Ch3 Section 8

Chapter 4

Ch4 Section 1

Ch4 Section 2

Ch4 Section 3

Ch4 Section 4

Ch4 Section 5

Ch4 Section 6

Ch4 Section 7

Ch4 Section 8

Ch4 Section 9

Ch4 Section 10

Ch4 Section 11

Chapter 5

Ch5 Section 1

Ch5 Section 2

Ch5 Section 3

Ch5 Section 4

Ch5 Section 5

Ch5 Section 6

Ch5 Section 7

Ch5 Section 8

Ch5 Section 9

Ch5 Section 10

Ch5 Section 11

Ch5 Section 12

Chapter 6

Ch6 Section 1

Ch6 Section 2

Ch6 Section 3

Ch6 Section 4

Ch6 Section 5

Ch6 Section 6

Ch6 Section 7

Ch6 Section 8

Ch6 Section 9

Chapter 7

Ch7 Section 1

Ch7 Section 2

Ch7 Section 3

Ch7 Section 4

Ch7 Section 5

Ch7 Section 6

Ch7 Section 7

Ch7 Section 8

Chapter 8

Ch8 Section 1

Ch8 Section 2

Ch8 Section 3

Ch8 Section 4

Ch8 Section 5

Ch8 Section 6

Ch8 Section 7

Appendix A

Appendix B

Appendix C

History

See also


External links

References

Footnotes


Comments