Skip to content
nbyinfinity
Go back

๐Ÿ“˜ K&R C Solutions

โ€ข 3 min read โ€ข ~483 words
๐Ÿ“˜ K&R C Solutions

Table of contents

Open Table of contents

๐Ÿ“– Introduction

Finally finished my long-pending read of The C Programming Language by Brian Kernighan and Dennis Ritchie, along with all the exercises in it.

High-level abstractions are a great help, but they also hide the beauty of what happens under the hood. Writing my own versions of I/O routines, string functions, a memory allocator, and bit manipulation utilities showed me what is really going on. Standard library calls stop feeling like magic black boxes.

The exercises make you pause and think about what could go wrong: empty input, EOF, buffer boundaries, and memory alignment etc. That is where most of the learning happened.

The book is barely over 200 pages, yet it packs a huge amount of core fundamentals.

โœ๏ธ Exercises and solutions

you can find my solutions in the kr-c-solutions repository on GitHub.

ChapterTitleFolder
Chapter 1A Introduction Introductionexercises/chapter-01/
Chapter 2Types, Operators, and Expressionsexercises/chapter-02/
Chapter 3Control Flowexercises/chapter-03/
Chapter 4Functions and Program Structureexercises/chapter-04/
Chapter 5Pointers and Arraysexercises/chapter-05/
Chapter 6Structuresexercises/chapter-06/
Chapter 7Input and Outputexercises/chapter-07/
Chapter 8The UNIX System Interfaceexercises/chapter-08/

โ–ถ๏ธ Run

Using make

ActionCommand
Build allmake
Build onemake ex-01-19
Build & runmake run FILE=ex-01-19.c
Run with stdin filemake run FILE=ex-01-19.c INPUT=test/input.txt
List programsmake list
Clean buildmake clean

Using VS Code (CodeLLDB)

Requires the CodeLLDB extension. Open any .c file under exercises/ and use:

StepActionDetails
BuildCommand Palette (โ‡งโŒ˜P) โ†’ Tasks: Run Build TaskRuns default task Build active file (clang โ†’ build/<name>) from .vscode/tasks.json
Debug / RunCommand Palette (โ‡งโŒ˜P) โ†’ Debug: Select and Start Debugging โ†’ select configurationRuns preLaunchTask (Build active file) then launches via lldb (.vscode/launch.json)

Available launch.json configurations:

ConfigurationInput / Args
Launch: no args (terminal stdin)Type stdin in integrated terminal (Ctrl+D for EOF)
Launch: prompt for argsPrompts for space-separated progArgs
Launch: prompt for stdin filePrompts for full path stdinFullPath
Launch: prompt for stdin file + argsPrompts for full path stdinFullPath and progArgs

๐Ÿ“ Notes

These solutions were written and tested on macOS.

๐Ÿ“š References


Share this post:

Next Post
๐Ÿ”ข Bitcount in C: Counting Set Bits Efficiently