Exercise 1-17

Write a program to print all input lines that are longer than 80 characters.

We begin by defining line and len to store input lines and their lengths. In order to read in lines, as shown in the longest-line program, we run getline within a while-loop until it returns a value of zero, which signals that it has read in an end-of-file.

    #include <stdio.h>
    
    #define MAXLINE 1000    /* maximum input line size */
    
    int my_getline(char line[], int maxline);
    
    main()
    {
        int len;    /* current line length */
        char line[MAXLINE]; /* current input line */
    
        while ((len = my_getline(line, MAXLINE)) > 0)
            /* code */;
        return 0;
    }

Now, we define the symbolic constant MIN to represent the minimum length for a line to be printed (you should always use symbolic constants in place of magic numbers such as 81.) For every line we read in, if len is greater than or equal to MIN, we print line.

    #include <stdio.h>
    
    #define MIN 81          /* minimum size for line to be printed */
    #define MAXLINE 1000    /* maximum input line size */
    
    int my_getline(char line[], int maxline);
    
    main()
    {
        int len;    /* current line length */
        char line[MAXLINE]; /* current input line */
    
        while ((len = my_getline(line, MAXLINE)) > 0)
            if (len >= MIN)
                printf("%s", line);
        return 0;
    }