Search code examples
ckernighan-and-ritchie

K&R, find longest line, same code but not working?


I've just started to read K&R and on pages 32-33, there is a code that

finds the longest line among the inputs.

I nearly completely copy-pasted the code given in the book, just added some comment lines to make the code more understandable for me. But it isn't working.

Edit: I'm sorry for bad questioning. It seems the program does not act properly when I press Ctrl + Z, in order to terminate it. No matter how many lines I type and how many times I press Ctrl + Z, it just does nothing.

The following is my version of the code:

/* Find the longest line among the giving inputs and print it */

#include <stdio.h>
#define MAXLINE 1000            /* maximum input line length */

int getLine(char line[], int maxLine);
void copy(char to[], char from[]);

int main(void) {
    int len; /* current line length */
    int max; /* maximum length seen so far */
    char line[MAXLINE]; /* current input line */
    char longest[MAXLINE]; /* longest line saved here*/

    max = 0;

    /* getLine function takes all the input from user, returns it's size and equates it to the variable len
     * Then, len is compared whether it's greater than zero because if there's no input, no need to do any calculation
     * EDGE CASE
     */
    while ((len = getLine(line, MAXLINE)) > 0)
        /* If the length of input is larger than the previous max length, set max as the new length value and copy that input */
        if (len > max) {
            max = len;
            copy(longest, line);
        }
    if (max > 0) /* there was a line, EDGE CASE */
        printf("%s", longest);

    return 0;
}

/* Read a line into s, return length.
 * Since the input length is unknown, there should be a limit */
int getLine(char s[], int lim) {
    int c, i;

    /* The loop's first condition is whether the input length is below the limit. EDGE CASE
     * If it's not, omit the rest because it would cause a BUFFER OVERFLOW. Next, take the input as long as it's not an EOF command.
     * Finally, if the input is end of line, finish the loop, don' take it.
     */
    for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; i++)
        s[i] = c;
    if (c == '\n')
        s[i++] = c;
    s[i++] = '\0'; // always put a '\0' character to a string array ending, so that the compiler knows it's a string.
    return i;
}

void copy(char to[], char from[]) {
    int i = 0;

    // This loop is readily assigns all chars from the source array to the target array until it reaches the ending char.
    while ((to[i] = from[i]) != '\0')
        ++i;
}

Thanks in advance!


Solution

  • Okay, here's the error:

    s[i++] = '\0'; // always put a '\0' character to a string array ending, so that the compiler knows it's a string.
    

    This will cause it to terminate the string even for no input (when it got EOF directly), and since it increments i before returning it, getLine() will never return 0 and thus main() will never stop. Removing the ++ fixed it, for my simple test.

    Also, the comment is misleading, the compiler doesn't know anything. The compiler is no longer around when the code runs; the in-memory format of strings is something that's needed to keep the run-time libraries happy since that's what they expect.