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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-01-2004, 02:34 AM
|
#1
|
LQ Newbie
Registered: Jul 2004
Posts: 27
Rep:
|
Did you guys learn C the way I'm learning C?
I was just wondering how many of you have learnt C by reading "The C Programming Language" and coding the exercises. Because that's what I'm doing right now. Previously, I had only read the book, but I didn't feel like I learnt anything (obviously), so I decided to actually do most (if not all) of the exercises -- is this a good strategy, or am I being too anal?
|
|
|
08-01-2004, 02:41 AM
|
#2
|
Senior Member
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Debian, FreeBSD
Posts: 1,450
Rep:
|
lol, I learned C the wrong(tm) way. All I had was a cheap compiler (on a 1.44 floppy that also had command.com on it to boot) and a huge freaking reference book. The book was nothing more than a huge reference manual for the language. My first few programs completely failed to work because I didn't know about headers or declaring functions. Lucky for me... the error gave me clues to check in the appendix and eventually I got things to compile. The book never mentioned that you needed to do stuff like that. Most of the sample code was for maybe a complete function but rarely a complete program -- although there were a few complete program examples that helped point me in the right direction.
The book was cool though. It had pretty much everything... and short sample segments of code showing how certain functions or commands worked. It had special sections on ANSI standards and C++ (which was slightly in the future for when the book was written)... ah... it was horrible and magical at the same time.
Learning C that way was like nailing jelly to a tree. And I can honestly mean that when I say it.
Edit:
No you are not being anal and you aren't going about it the wrong way. The only way to learn to code is to do it. Whether by playing with example programs from a book of blindly plowing ahead without a direction or hope of success. Code my man... code! Until your fingers bleed and then some.
Last edited by frob23; 08-01-2004 at 02:44 AM.
|
|
|
08-01-2004, 05:30 AM
|
#3
|
Member
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419
Rep:
|
If the book you are talking about is, "The C programming Language" by KnR, then
Id suggest getting a thicker C book that spends more time explaining things.
That book is a good read AFTER youve learned the language, since its pretty
light on details, IMHO.
|
|
|
08-01-2004, 05:37 AM
|
#4
|
Senior Member
Registered: Nov 2002
Location: pikes peak
Distribution: Slackware, LFS
Posts: 2,577
Rep:
|
De-bugging other people's code is also a way of learning C
Like Linux programs, I'm always have to fix "syntax" errors of
other peoples source code so it will compile properly
|
|
|
08-01-2004, 11:11 AM
|
#5
|
Member
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557
Rep:
|
I learned C as my first programming language by reading k&r and doing *some* of the excersizes. Eventually you'll want to just dive in and program your own stuff, because excersizes are boring. At any rate, it can be done.
I don't like the idea of learning by debugging other people's stuff, particularly with C, because it is easy to get something that compiles and appears to work, but is actually broken. Examples of such traps are writing past the end of arrays, writing to read-only memory, not free()ing memory, relying on endianness, relying on sizes of built in data types, and casting without knowing what you're doing. All of the above can work under certain circumstances, and all are really bad.
Then again, everybody learns differently. I'm a bit of a book worm.
|
|
|
08-01-2004, 12:56 PM
|
#6
|
LQ Newbie
Registered: Jul 2004
Posts: 27
Original Poster
Rep:
|
Randomly Generated Tests
Thank you for the replies thus far.
frob23 -- "nailing jelly to a tree" -- that's pretty funny, it sounds like you're very motivated. Thanks for the inspiration
I'm learning C mainly because I like it and think it will make for a strong foundation (when I get into a specific area -- like computer graphics, or whatever). And I'm learning a lot more than just C by going through the process of solving the exercises. For example, I've read about several programming methodologies (like extreme programming, or test-driven development), and the recurring theme seems to be to write tests, so that's what I'm doing, too.
For example, to test two of the exercises (exercise 20 and 21: detab and entab respectively), I eventually (after some thought and after failing with testing them manually) wrote the following python script which randomly generates a test:
Code:
#!/usr/bin/python
import random
# i've had some success before with using
# randomly generated tests -- it had
# discovered some bugs in a program
# when i thought it was well tested,
# until i tried the random approach,
# that is.
def gen_test_data(nchars):
# this function basically prints
# a bunch of characters, with blanks
# and newlines among them
# (NO tabs however)
f = open( "205.test", "w" )
for i in range(nchars):
u = random.uniform(0,1)
if u < 0.05:
f.write('\n')
elif u < 0.85:
f.write(' ')
else:
u = random.randrange(33,127)
f.write(chr(u))
f.write('\n')
f.close()
# generate a test file to test 20.detab and 21.entab
# the resultant test file is fed into 21.entab first,
# then the result into 20.detab and then
# the result of that is compared with the original
# test file -- they should match
gen_test_data(1000)
And then I added the following to my Makefile:
Code:
test205:
./205.maketest.py
./21.entab < 205.test > 21.result
./20.detab < 21.result > 20.result
diff 205.test 20.result
so as to automate the process of testing. I can then just type 'make test205' to test two programs at once with a randomly generated test.
What do you guys think? Good idea? I just though I'd post it, because I felt as though I learnt a valuable lesson. Maybe some other programming newbie will find this useful, too.
|
|
|
08-01-2004, 01:25 PM
|
#7
|
Member
Registered: Mar 2003
Posts: 804
Rep:
|
i think you're learning excellent programming habits and will make a fine programmer if u keep at it  automated testing is something a lot more programmers need to start doing.
|
|
|
08-01-2004, 04:12 PM
|
#8
|
Member
Registered: Jul 2004
Location: Spain
Posts: 111
Rep:
|
You're doing the same i did, I almost read the book at first (i didn't do any exercise), but when i realized that i wasn't be able to do the hard exercises, i start again, doing all exercises. Then, they were easiest than i thought.
|
|
|
All times are GMT -5. The time now is 09:51 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|