K&R C Chapter 2 Exercise Solutions — Types, Operators, and Expressions

Chapter 2: Types, Operators, and Expressions covers C’s type system and operators in depth. The exercises focus on bitwise manipulation — rotating bits, setting and clearing fields, counting set bits — and type-conversion edge cases. Exercise 2-9 (x &= (x-1) deletes the rightmost 1-bit) is a classic bit-manipulation trick worth memorising. These are worked solutions …

K&R C Chapter 1 Exercise Solutions — A Tutorial Introduction

Chapter 1: A Tutorial Introduction is where K&R C begins — and it moves faster than most C books. By exercise 1-8 you are counting characters by category; by 1-13 you are drawing histograms; by 1-22 you are folding long lines. The chapter uses only a tiny subset of C deliberately: loops, if/else, basic functions, …

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 …

K&R C Programs Exercise 7-7

Exercise 7-7. Modify the pattern finding program of Chapter 5 to take its input from a set of named files or, if no files are named, from the standard input. Should the file name be printed when a match is found? Yes — when searching multiple files the filename should prefix each matching line (the …

K&R C Programs Exercise 7-6

Exercise 7-6. Write a program to compare two files, printing the first line where they differ. Open both files, read them line by line in parallel, and compare. When a differing line is found, print both versions with a line number and exit. Also handle the case where one file is longer than the other. …