LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Question on K&R's code example in section 1.9 (https://www.linuxquestions.org/questions/programming-9/question-on-k-and-rs-code-example-in-section-1-9-a-504295/)

oneandoneis2 11-23-2006 03:17 PM

Question on K&R's code example in section 1.9
 
I've been trying to work this out for hours, and I'm just not getting it.

The code for "getline" they supply in this section:
Code:

        for (i=0; i<lim-1 && (c=getchar()) != EOF && c!='\n'; ++i)
                s[i] = c;
        if (c == '\n')  {
                s[i] = c;
                ++i;
        }
        s[i] = '\0';
        return i;

I don't understand why the for loop tests the character to see if it's '\n'. Why didn't they use:
Code:

        for (i=0; i<lim-1 && (c=getchar()) != EOF; ++i)
                s[i] = c;
        }
        s[i] = '\0';
        return i;

With their code, they single out the '\n' to stop the for loop, which would otherwise set s[i] to '\n' and increment i by one; and instead have the if statement set s[i] to '\n' and increment i by one.

What's the difference??

There must be something I'm missing, because they specifically wrote the code this way. So what's going on? What am I missing?

Thanks in advance

oneandoneis2 11-23-2006 03:52 PM

Never mind, I finally figured it out:

It's because you want to work on each line individually, right? If you didn't terminate the loop at '\n', it would keep counting characters until an EOF was encountered.

This is "getline", not "getinput"

Seems so obvious now. . .

indienick 11-23-2006 04:40 PM

hmm...I've read this over and over about 15 times now, and I can't figure it out either (keep in mind, C is a mystery to me - I stick to LISP).

I'm thinking perhaps it's a way of forcing the user to input a character that's NOT an EOF or newline; but other blank characters like, tabs, spaces, etc., are legal. hmm.....

*shrug* I suppose this could be so that the s[] array isn't containing more than one of the same value (so there won't be any more than one '\n').

at least that's a possible explanation for what's happening in the FOR loop.

when that IF conditional comes in, that's where it seems to lose all rationality. literally, it's going (starting at the FOR loop):
"loop while c is not an EOF char, or a newline char, and should c be a newline char, exit this loop, and don't assign it to the current array element.
if c is a newline char, assign it to the current array element."

alright...I just re-read it and read that it's a "getline" example.
this makes sense now.

pretty much, the loop will go as long as a non-newline or EOF char is input, and assign each char to an array element. the FOR loop exits when a newline or EOF char is passed. it will kick out of the loop so no error or glitch arises, and if the char passed is a newline, the IF condition makes sure that it's that last character in that char array.

:) I hope this answers it for you.

indienick 11-23-2006 04:42 PM

haha, you replied in the middle of me typing out my reply. :D


All times are GMT -5. The time now is 06:28 AM.