ckernighan-and-ritchiec89

K&R Exercise 1.16 - Limitation on line length


I'm learning C from K&R's "The C Programming Language" book. I'm doing the exercises specified in the book. I'm on exercise number 1.16, but I don't understand it.

Exercise 1.16:

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

My questions:

  1. "...as much as possible of the text..." - is there some limitation on string length? Maybe in standard headers there's a variable with the max allowed value of string length?

  2. "...the length of arbitrarily long input lines..." - but in the code MAXLINE is defined as 1000. It is limited size too. I see some solutions here, but in my opinion it is not solution decision, since on the former there is a restriction on length of a line (1000 characters).

Maybe I don't understood the task. My understanding is I must remove the 1000-character limitation.


Solution

  • It's a pretty early exercise in K&R, you're just supposed to do some minor changes to the code, not a total redesign of the code.

    1. "...as much as possible of the text..."

      is up to you to interpret. I'd do it by printing what's stored in the longest buffer. i.e. print out up to 1000 characters of the line. Again, it's an early exercise, with little introduction to dynamically allocated memory yet. And at the time K&R was written, storing away arbitrarily long text lines wasn't as feasible as it is today.

    2. "...the length of arbitrarily long input lines..."

      Is a hard requirement. You're supposed to find the correct length no matter how long it is (at least within the bounds of an int. )

    One way to solve this problem is:

    I'll not spoil it and post the code you need to accomplish this, but it is just 6 lines of code that you need to add to the longest-line program of exercise 1-16.