K & R C Exercise Solutions.

We have already provided solutions to all the exercises in the book “C Programming Language (2nd Edition)“ popularly known as K & R C book. In this blog post I will give links to all the chapters of the “C Programming Language (2nd Edition)” popularly known as K & R C book for easy reference. Chapter …

K&R C Chapter 7 Exercise Solutions — Input and Output

Chapter 7: Input and Output covers the standard I/O library in depth — formatted output with printf, formatted input with scanf, file access with fopen/fclose, line I/O with fgets/fputs, and error handling via stderr. The chapter also introduces variable-argument functions (stdarg.h) through a worked minprintf example. The exercises range from practical utilities (a case-converter, a …

K&R C Chapter 6 Exercise Solutions — Structures

Chapter 6: Structures introduces struct, typedef, and self-referential structures (linked lists, binary trees). The chapter builds a word-frequency counter using a binary tree, then extends it with a hash table. Exercise 6-3 adds a cross-reference listing; 6-6 implements a rudimentary #define processor using a hash table — the most complex exercise in this chapter. These …

K&R C Chapter 3 Exercise Solutions — Control Flow

Chapter 3: Control Flow covers C’s branching and looping constructs. It is short (6 exercises) but the exercises are non-trivial: exercise 3-3 implements expand(s1,s2) to expand shorthand like a-z into the full alphabet; 3-4 handles itoa for the most negative integer (a classic overflow trap); 3-5 converts integers to arbitrary bases. These are worked solutions …

K&R C Programs Exercise 7-9

Exercise 7-9. Functions like isupper can be implemented to save space or to save time. Explore both possibilities. Standard library character-classification functions like isupper are typically implemented as macros. There are two design axes: Save time: use a precomputed lookup table — one array access, no branches, O(1) at maximum cache efficiency Save space: use …

K&R C Programs Exercise 7-8

Exercise 7-8. Write a program to print a set of files, starting each new file on a new page, with a title and a running page count for each file. Print each file with a header line (filename + page number) at the top of every page. A page is LINES_PER_PAGE lines. When a page …