/knrc

Examples and exercises from second edition K&R

Primary LanguageC

Notes on K&R C (2nd edition)

This repository contains examples and exercises from the second edition K&R.

  • Compiled using gcc version 8.3.0 on Debian 10 x86_64

01-hello-world.c

  • A return type is explicitly set on the main function to clear a gcc warning in a slight deviation from the book.

This example demonstrates an understanding of how to compile source code to machine code. It includes the 01-hello-world.c source file and a Makefile that executes a gcc command to compile. In addition, a .gitignore file is included in the ./bin directory to keep binary artifacts out of version control.

Exercise 1-1: leave out parts

While these omissions are not directly included in the source, here are some examples of leaving parts out to generate compilation errors or warnings.

Removing braces:

$ sed -i 's/[{}]//g' 01-hello-world.c
$ make
gcc -o ./bin/01-helloworld 01-hello-world.c
01-hello-world.c: In function ‘main’:
01-hello-world.c:5:2: error: expected declaration specifiers before ‘printf’
  printf("hello, world\n");
  ^~~~~~
01-hello-world.c:6: error: expected ‘{’ at end of input


make: *** [Makefile:4: hello] Error 1

Removing the semicolon:

$ sed -i 's/[;]//g' 01-hello-world.c
$ make
gcc -o ./bin/01-helloworld 01-hello-world.c
01-hello-world.c: In function ‘main’:
01-hello-world.c:5:26: error: expected ‘;’ before ‘}’ token
  printf("hello, world\n")
                          ^
                          ;
 }
 ~
make: *** [Makefile:4: hello] Error 1

Removing the include directive:

$ sed -i 's/#include.*//g' 01-hello-world.c
$ make
gcc -o ./bin/01-helloworld 01-hello-world.c
01-hello-world.c: In function ‘main’:
01-hello-world.c:5:2: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
  printf("hello, world\n");
  ^~~~~~
01-hello-world.c:5:2: warning: incompatible implicit declaration of built-in function ‘printf’
01-hello-world.c:5:2: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
01-hello-world.c:1:1:
+#include <stdio.h>

01-hello-world.c:5:2:
  printf("hello, world\n");
  ^~~~~~
$ ./bin/01-helloworld
hello, world
$

Exercise 1-2: unknown escapes in printf

Adding a \k into printf:

$ sed -i 's/\\n/\\k\\n/g' 01-hello-world.c
$ make
gcc -o ./bin/01-helloworld 01-hello-world.c
01-hello-world.c: In function ‘main’:
01-hello-world.c:5:9: warning: unknown escape sequence: '\k'
  printf("hello, world\k\n");
         ^~~~~~~~~~~~~~~~~~
$ ./bin/01-helloworld
hello, worldk
$

02-vars-and-math.c

This example incorporates comments, declaring variables, variable assignments, the while loop, and displaying arithmetic results into stdout.

Higher precision

After typing out the second program’s source, I instantly noticed a lack of floating-point numbers (from previous knowledge). So, impatiently, I came up with a solution for a cleaner and higher precision conversion table before seeing the book’s answer. This slight change involved changing the variables' type to float and changing %d to %g in printf.

Match textbook formatting

Conform to the textbook example, using better formatting and floating-point numbers throughout the equation.

The for loop

  • I skipped over exercises 1-3 and 1-4 into the for loop in a slip-up

Here we simplify the code into a for loop, removing all but one variable.

Exercise 1-3: print a heading

I add a heading above the table with an additional printf statement and made a slight adjustment in the original printf to align it better.

Exercise 1-4. print Celsius to Fahrenheit table

This is a simple little solution for a Celsius to Fahrenheit table.

Exercise 1-5. print the table in reverse order

Move some numbers around to reverse the table.

09-word-count.c

Textbook example of counting lines, words, and characters from input.

Exercise 1-11: test the word count program

No input:

$ ./bin/09-word-count
0 0 0
$

Just 3 newlines:

$ ./bin/09-word-count



3 0 3
$

Just 3 tabs:

$ ./bin/09-word-count
                        0 0 3
$

Just 3 spaces:

$ ./bin/09-word-count
   0 0 3
$

Just a single word per line:

$ ./bin/09-word-count
one
word
per
line
4 4 18
$

Three blanks before and after:

$ ./bin/09-word-count
   three blanks before/after   0 3 31
$