LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-23-2006, 03:17 PM   #1
oneandoneis2
Senior Member
 
Registered: Nov 2003
Location: London, England
Distribution: Ubuntu
Posts: 1,460

Rep: Reputation: 48
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
 
Old 11-23-2006, 03:52 PM   #2
oneandoneis2
Senior Member
 
Registered: Nov 2003
Location: London, England
Distribution: Ubuntu
Posts: 1,460

Original Poster
Rep: Reputation: 48
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. . .
 
Old 11-23-2006, 04:40 PM   #3
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
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.
 
Old 11-23-2006, 04:42 PM   #4
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
haha, you replied in the middle of me typing out my reply.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
RPM Spec file creation, %files section question davidas Linux - Software 1 09-13-2007 02:26 AM
Can someone explain to me this code from K&R's 'The C Programming Language' book? frankie_DJ Programming 10 11-25-2006 01:35 PM
Is K&R's "The C Programming Language" outdated? frankie_DJ Programming 3 09-21-2006 07:39 AM
rpmbuild - build section question jcalcote SUSE / openSUSE 3 03-12-2005 06:10 AM
How do I formatt CD-R'S in Linux EliotK Linux - Software 2 03-03-2005 03:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration