Question

I am going through the Kernighan and Ritchie's The C Programming Language, Second Edition and I am having a problem understanding the nature of the assignment in Exercise 1-16. I've read it over many times but cannot figure out exactly what they are asking of me; forgive me but English is not my first language.

The exercise description is:

Revise the main routine of the longest-line program so that it will correctly print the length of arbitrarily long input lines and as much as possible of the text.

Does this mean that the program should no longer be constrained by the MAXLINE string limitation when it comes to the length of the longest line or that it wants you to print every line alone with their lengths? If it's the latter, does it want you to print each line after it is entered along with its length or just the lengths?

The two options I've come up with are quite different so I would really appreciate some advice.

Was it helpful?

Solution

  1. Does this mean that the program should no longer be constrained by the MAXLINE string limitation?

No, it doesn't. At this point in the book, you're not supposed to know how to handle and store lines with an arbitrary length. Chapter 1 is a very superficial introduction to C, it is not expected that you redesign the whole code.

With that in mind: the exercise is asking you to print as much as you can of the line, i.e., if the line is longer than MAXLINE characters, you just print the first MAXLINE. But you should also print the length of the original line. In other words, you store as much as you can up to MAXLINE characters, but you keep reading after that limit just for the purpose of counting.

To do this, the getline() function must be updated to keep counting the chars until the end of line, even if it reached the limit of the buffer, in which case it counts but it obviously doesn't write the text to the buffer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top