K&R C Programming Exercise Solutions — Complete Guide (Kernighan & Ritchie)

The C Programming Language by Brian W. Kernighan and Dennis M. Ritchie — universally known as K&R C — is the definitive reference for the C language. First published in 1978, the ANSI C second edition (1988) remains the gold standard for learning C correctly: terse, precise, and written by the language’s own creators.

This page is a complete index of worked solutions to all exercises across all 7 chapters of the 2nd edition. Each solution is written in standard ANSI C, compiles cleanly with gcc, and includes the exercise statement for context.

Get the book: The C Programming Language, 2nd Edition — Brian W. Kernighan & Dennis M. Ritchie (Amazon.in)


Exercise Solutions by Chapter

Chapter Topic Exercises Key Concepts
Chapter 1 A Tutorial Introduction 26 Hello World, loops, functions, character I/O, word counting
Chapter 2 Types, Operators, and Expressions 10 Data types, bitwise operators, ternary expressions, type conversions
Chapter 3 Control Flow 6 if-else, switch, loops, break/continue, binary search
Chapter 4 Functions and Program Structure 14 Recursion, scope, header files, static variables, reverse Polish calculator
Chapter 5 Pointers and Arrays 20 Pointer arithmetic, array/pointer equivalence, command-line arguments, sorting
Chapter 6 Structures 6 struct, linked lists, hash tables, self-referential structures, typedef
Chapter 7 Input and Output 9 printf/scanf format strings, file I/O, error handling, line I/O

Total: 91 exercises covering all topics in the 2nd edition. Chapter 8 (The UNIX System Interface) is system-specific and not included.


Individual Exercise Index

Chapter 1 — A Tutorial Introduction

  1. Exercise 1-1a
  2. Exercise 1-1b
  3. Exercise 1-1c
  4. Exercise 1-2
  5. Exercise 1-3
  6. Exercise 1-4
  7. Exercise 1-5
  8. Exercise 1-6
  9. Exercise 1-7
  10. Exercise 1-8
  11. Exercise 1-9
  12. Exercise 1-10
  13. Exercise 1-11
  14. Exercise 1-12
  15. Exercise 1-13
  16. Exercise 1-14
  17. Exercise 1-15
  18. Exercise 1-16
  19. Exercise 1-17
  20. Exercise 1-18
  21. Exercise 1-19
  22. Exercise 1-20
  23. Exercise 1-21
  24. Exercise 1-22
  25. Exercise 1-23
  26. Exercise 1-24

Chapter 2 — Types, Operators, and Expressions

  1. Exercise 2-1
  2. Exercise 2-2
  3. Exercise 2-3
  4. Exercise 2-4
  5. Exercise 2-5
  6. Exercise 2-6
  7. Exercise 2-7
  8. Exercise 2-8
  9. Exercise 2-9
  10. Exercise 2-10

Chapter 3 — Control Flow

  1. Exercise 3-1
  2. Exercise 3-2
  3. Exercise 3-3
  4. Exercise 3-4
  5. Exercise 3-5
  6. Exercise 3-6

Chapter 4 — Functions and Program Structure

  1. Exercise 4-1
  2. Exercise 4-2
  3. Exercise 4-3
  4. Exercise 4-4
  5. Exercise 4-5
  6. Exercise 4-6
  7. Exercise 4-7
  8. Exercise 4-8
  9. Exercise 4-9
  10. Exercise 4-10
  11. Exercise 4-11
  12. Exercise 4-12
  13. Exercise 4-13
  14. Exercise 4-14

Chapter 5 — Pointers and Arrays

  1. Exercise 5-1
  2. Exercise 5-2
  3. Exercise 5-3
  4. Exercise 5-4
  5. Exercise 5-5
  6. Exercise 5-6
  7. Exercise 5-7
  8. Exercise 5-8
  9. Exercise 5-9
  10. Exercise 5-10
  11. Exercise 5-11
  12. Exercise 5-12
  13. Exercise 5-13
  14. Exercise 5-14
  15. Exercise 5-15
  16. Exercise 5-16
  17. Exercise 5-17
  18. Exercise 5-18
  19. Exercise 5-19
  20. Exercise 5-20

Chapter 6 — Structures

  1. Exercise 6-1
  2. Exercise 6-2
  3. Exercise 6-3
  4. Exercise 6-4
  5. Exercise 6-5
  6. Exercise 6-6

Chapter 7 — Input and Output

  1. Exercise 7-1
  2. Exercise 7-2
  3. Exercise 7-3
  4. Exercise 7-4
  5. Exercise 7-5
  6. Exercise 7-6
  7. Exercise 7-7
  8. Exercise 7-8
  9. Exercise 7-9

About The C Programming Language

The C Programming Language (2nd edition, 1988) by Brian Kernighan and Dennis Ritchie is the book that defined how C is taught and written. Ritchie created C at Bell Labs in 1972; Kernighan co-developed it and wrote the majority of the book. It is compact (272 pages), precise, and still the most reliable reference for ANSI C.

If you are learning C seriously, working through the exercises is essential. The exercises build on each other — by Chapter 5 you are implementing sorting algorithms and string functions from scratch using only pointers and arrays. These solutions are meant to be studied, not copied: understanding why each solution works is the goal.

See also: Complete C Programs List | C Aptitude Questions