Exercise 1-17. Write a program to print all input lines that are longer than 80 characters.
Approach
This exercise is a direct application of the getline / main pattern introduced in K&R Section 1.9 — but now with a filter condition added to main. The key insight is that getline already returns the line length, so the filter is a single comparison: len > LONGLINE. There is no need to call strlen().
Two details are worth watching. First, getline is a name reserved by POSIX, so the function is renamed getline2 to avoid conflicts. Second, the condition is strictly greater than 80 — a line of exactly 80 characters is not printed. Defining the threshold as a named constant (LONGLINE 80) makes it trivial to adjust later.
Solution
/* K&R Exercise 1-17: print lines longer than 80 characters */
/* Compile: gcc -ansi -Wall exercise1-17.c -o exercise1-17 */
#include <stdio.h>
#define MAXLINE 1000 /* maximum input line length to store */
#define LONGLINE 80 /* threshold: lines longer than this printed */
int getline2(char line[], int maxline);
int main(void)
{
int len;
char line[MAXLINE];
while ((len = getline2(line, MAXLINE)) > 0)
if (len > LONGLINE)
printf("%s", line);
return 0;
}
/* getline2: read a line into line[], return its length */
int getline2(char line[], int maxline)
{
int c, i;
for (i = 0; i < maxline - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
line[i] = c;
if (c == '\n') {
line[i] = '\n';
++i;
}
line[i] = '\0';
return i;
}
Compile and Run
gcc -ansi -Wall exercise1-17.c -o exercise1-17
./exercise1-17
The program reads from standard input and writes matching lines to standard output — the classic Unix filter pattern. Type lines at the terminal (Ctrl-D to end), or pipe input from a file or command:
# Generate a test line of 85 'a' characters and pipe it in
python3 -c "print('a'*85)" | ./exercise1-17
# Or use a file
./exercise1-17 < somefile.txt
Sample Output
Input (four lines): Short line. (11 chars — not printed) Exactly eighty characters long, padded to fill it precisely here, yes it is. (80 chars — not printed) This line is eighty-one characters long — one past the limit — so it appears. (81 chars — PRINTED) Another very long line that clearly exceeds eighty characters and should appear in output. (91 chars — PRINTED) Output: This line is eighty-one characters long — one past the limit — so it appears. Another very long line that clearly exceeds eighty characters and should appear in output.
Edge Cases
- Exactly 80 characters: not printed — the condition is
len > LONGLINE, not>=. - Empty lines:
getline2returns 0 for an empty line (just a newline); thewhileloop exits cleanly. - Lines longer than MAXLINE: characters beyond position 998 are silently discarded. For production code, consider reading in chunks; for this exercise the large buffer is fine.
- No trailing newline at end of file: the
if (c == '\n')guard handles this — the final partial line is still returned and counted correctly.
What This Exercise Teaches
- The filter pattern: stdin → process → stdout. The program prints nothing unless a line qualifies. This is the foundation of Unix pipelines (
grep,awk,sed). - Reusing a function from the book:
getline2is lifted almost verbatim from Section 1.9. The exercise trains you to adapt existing building blocks rather than rewrite from scratch. - Using the return value as metadata:
getline2returns the length as part of its contract.mainexploits this directly — there is no separatestrlen()call. - Named constants for thresholds:
#define LONGLINE 80means changing the threshold to 120 requires editing exactly one line.
Set Up Your C Environment
To compile and run this solution you need GCC. If you haven’t set it up yet:
- Install GCC on Windows 11
- Install GCC on macOS
- Install GCC on Ubuntu / Linux
- VS Code for C Programming — recommended editor
- Complete C Development Environment Setup
← Exercise 1-16 |
Chapter 1 Solutions |
Exercise 1-18 →
Book:
The C Programming Language, 2nd Ed — Kernighan & Ritchie