Solutions for the Kernighan and Ritchie

F

freeasinfreedom

Hi everyone,

I've read the Kernighan and Ritchie some time ago, and then again, but
this time I've done the full set of exercises. I figured it would be
nice to share them :

http://linkefong.free.fr/downloads/tcpl/tcpl-solutions-1.0.zip

Your comments are welcome.

I've of course didn't peek in Tondo and Gimpel's book or Richard
Heathfield.

Regards,

Nice!

Since you like sharing your knowledge, you might be interested in
contributing to the free software community by adding you project to
savannah (savannah.gnu.org). In this way you could educate many
people.

Happy Hacking!
 
A

anotherlin

I'll be glad to see them added to the clc-wiki. The admin has my
blessing to upload them.

I've basically answered all exercises, even those missing from
Richard's collection.

But first, is there somebody to check them for correctness? Any
comments?
 
F

Flash Gordon

I'll be glad to see them added to the clc-wiki. The admin has my
blessing to upload them.

Thanks for getting his/her blessing first. Please include a link to the
original site when uploading them.
I've basically answered all exercises, even those missing from
Richard's collection.

But first, is there somebody to check them for correctness? Any
comments?

After uploading them they can be edited. So I would suggest upoading
them then posting links to them here asking for comment.
 
B

Ben Bacarisse

Hi everyone,

I've read the Kernighan and Ritchie some time ago, and then again, but
this time I've done the full set of exercises. I figured it would be
nice to share them :

http://linkefong.free.fr/downloads/tcpl/tcpl-solutions-1.0.zip

Your comments are welcome.

Looks good. That was a quick glance and if I have time I'll look
again. On one style issue, I was baffled by the large number of blank
lines. I won't make a fuss about style but it looked odd to me.

I spent a bit more time on one that I am currently familiar with (the
RPN calculator from chapter 4) and I am disappointed to see you also
have lost the original (correct in my opinion) K&R design of passing
getop the buffer size so you can prevent overflow. I'd count this as
a error.
 
U

user923005

Hi everyone,

I've read the Kernighan and Ritchie some time ago, and then again, but
this time I've done the full set of exercises. I figured it would be
nice to share them :

http://linkefong.free.fr/downloads/tcpl/tcpl-solutions-1.0.zip

Your comments are welcome.

I've of course didn't peek in Tondo and Gimpel's book or Richard
Heathfield.

The test driver for exercise 4.2 does not test the atof() function:

int main ()

{
printf("%g\n", 123.45e-6);
return 0;
}

Probably, you meant to do this:

#include <stdio.h>
int main ()

{
printf("%g\n", atof("123.45e-6"));
return 0;
}
 
R

Richard Bos

[ Edited for honesty ]
Since you like sharing your knowledge, you might be interested in
contributing to the free software community by adding you project to
spammy (spamspam.spam.gnu.org).

Or rather, do not, since I cannot find an Earthly reason why anyone
would think answers to K&R exercises have anything to do with the
contents of that page.

Freedom? Freedom to advertise where it isn't wanted, apparently.

Richard
 
A

anotherlin

Looks good.  That was a quick glance and if I have time I'll look
again.  On one style issue, I was baffled by the large number of blank
lines.  I won't make a fuss about style but it looked odd to me.

Well, I prefer things well separated. I use vi inside an 80x70
characters
xterm so there doesn't seem to be so much of them. I agree things can
look
differently and styles are a matter of personal taste.
I spent a bit more time on one that I am currently familiar with (the
RPN calculator from chapter 4) and I am disappointed to see you also
have lost the original (correct in my opinion) K&R design of passing
getop the buffer size so you can prevent overflow.  I'd count this as
a error.

I started with the getop() function p78 of my 2nd edition book (41st
printing)
and there is no buffer size (prototype is int getop (char s[])). In
fact, none
of the exercises of this chapter specifically ask for checking buffer
overflow.
However, I agree that it would have been better a better answer.
 
A

anotherlin

The test driver for exercise 4.2 does not test the atof() function:
[...]

Yes, you're right. I made a mistake and actually the atof() is not
correctly
tested and it has a bug! :( The exponent is not handled correctly, it
can be
fixed easily however. Thank you for pointing out this thing.
 
A

anotherlin

I'll be glad to see them added to the clc-wiki. The admin has my
Thanks for getting his/her blessing first. Please include a link to the
original site when uploading them.

Ok, English is not my main language (I live in France). By "blessing",
I
meant "I authorize the admin to put the content of the zip archive on
the
clc-wiki. I do so very *happily*." Hence the "blessing". I apologize
if I
may have offended you.

I read wikis a lot for various topics. But so far, I do not know how
to
write on them. And besides, it may be good first to check if the
proposed
answers are correct (exercise 4-2 has a bug as pointed out) before
putting
them on the wiki? Unless, the way is to put on the wiki and then
correct
them as needed?
 
B

Ben Bacarisse

I spent a bit more time on one that I am currently familiar with (the
RPN calculator from chapter 4) and I am disappointed to see you also
have lost the original (correct in my opinion) K&R design of passing
getop the buffer size so you can prevent overflow.  I'd count this as
a error.

I started with the getop() function p78 of my 2nd edition book (41st
printing)
and there is no buffer size (prototype is int getop (char s[])). In
fact, none
of the exercises of this chapter specifically ask for checking buffer
overflow.
However, I agree that it would have been better a better answer.

Well that explains why so many people have the same "fault". In my
K&R (I don't have the 2nd edition) getop takes a char * and an int.
Hardly surprising, the code check to ensure that the buffer passed is
never overrun.

I can't imagine why they'd go backwards, but it solves the mystery of
so many solutions that look, to me, like backward steps!
 
F

Flash Gordon

Ok, English is not my main language (I live in France). By "blessing",
I
meant "I authorize the admin to put the content of the zip archive on
the
clc-wiki. I do so very *happily*." Hence the "blessing". I apologize
if I
may have offended you.

There was no offence taken. I miss-read what you said as saying that the
admin of the site where they currently reside had given his/her blessing.

Oh, and I only have admin access because I host it. I don't have any
special control in terms of what is posted to the Wiki.
I read wikis a lot for various topics. But so far, I do not know how
to
write on them. And besides, it may be good first to check if the
proposed
answers are correct (exercise 4-2 has a bug as pointed out) before
putting
them on the wiki? Unless, the way is to put on the wiki and then
correct
them as needed?

Wiki's are editable, so you can put them up and correct them. You do
have to create yourself an account first.
 
Joined
Dec 17, 2008
Messages
1
Reaction score
0
New To Kr

i m knew to C. can i know aerent the putchar and getchar are used in file handling ?? thn how in KR book it is being used in the first chapter itself ? shouldn't the operations like counting words etcbe performed on some file?:damnmate:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top