K&R C Programs Exercise 6-3

Exercise 6-3. Write a cross-referencer that prints a list of all words in a document, and for each word, a list of the line numbers on which it occurs. Remove noise words like “the”, “and”, and so on from the list. The data structure is a binary tree where each node holds the word and …

K&R C Programs Exercise 6-2

Exercise 6-2. Write a program that reads a C program and prints in alphabetical order each group of variable names that are identical in the first 6 characters, but different somewhere thereafter. Don’t count words within strings and comments. Make 6 a parameter. The strategy: Use the improved getword from Exercise 6-1 to read identifiers, …

K&R C Programs Exercise 6-1

Exercise 6-1. Our version of getword does not properly handle underscores, string constants, comments, or preprocessor control lines. Write a better version. K&R’s getword from Section 6.3 reads the next identifier or non-alphabetic character from input, but has four gaps: Underscores: valid in C identifiers (_var, size_t) but the original checks only isalpha String constants: …

K&R C Programs Exercise 5-20

Exercise 5-20. Expand dcl to handle declarations with function argument types, qualifiers like const, and so on. The basic dcl treats () as “function returning” but ignores argument types. This exercise adds: parameter lists inside (), type qualifiers (const, volatile), storage classes (static, extern), and the void type. The approach: when ( is encountered and …

K&R C Programs Exercise 5-19

Exercise 5-19. Modify undcl so that it does not add redundant parentheses to expressions. K&R’s undcl (Section 5.12) converts English descriptions back into C declarations. The original adds parentheses around every pointer-to-function construct, even when they are not needed. A pointer to a function needs parentheses — int (*fp)(); a simple pointer does not — …

K&R C Programs Exercise 5-18

Exercise 5-18. Make the basic dcl program recover from input errors. K&R’s dcl program (Section 5.12) parses C declarations into English. It calls error() which prints a message and then exits — making it useless for batch input. The fix: on error, skip to the next newline and try to continue. This requires turning dcl‘s …