LinuxQuestions.org
Help answer threads with 0 replies.
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 08-01-2004, 02:34 AM   #1
psiakr3w
LQ Newbie
 
Registered: Jul 2004
Posts: 27

Rep: Reputation: 15
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?
 
Old 08-01-2004, 02:41 AM   #2
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: Ubuntu, FreeBSD, NetBSD
Posts: 1,449

Rep: Reputation: 47
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.
 
Old 08-01-2004, 05:30 AM   #3
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 417

Rep: Reputation: 31
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.
 
Old 08-01-2004, 05:37 AM   #4
320mb
Senior Member
 
Registered: Nov 2002
Location: pikes peak
Distribution: Slackware, LFS
Posts: 2,577

Rep: Reputation: 47
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
 
Old 08-01-2004, 11:11 AM   #5
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
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.
 
Old 08-01-2004, 12:56 PM   #6
psiakr3w
LQ Newbie
 
Registered: Jul 2004
Posts: 27

Original Poster
Rep: Reputation: 15
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.
 
Old 08-01-2004, 01:25 PM   #7
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
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.
 
Old 08-01-2004, 04:12 PM   #8
Nerox
Member
 
Registered: Jul 2004
Location: Spain
Posts: 111

Rep: Reputation: 20
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Hi guys, need a little help!!!!!Just started learning C MoeedK Programming 4 08-10-2005 07:41 PM
Hello, fairly new Linux user, looking forward to learning from you guys kenscottiii LinuxQuestions.org Member Intro 3 11-28-2004 05:03 AM
What Now Guys! dannyflounders Slackware 10 07-29-2003 08:59 AM
what do you guys think about this? roofy General 5 05-23-2003 01:40 AM
Need you help guys! eraser Programming 1 10-13-2000 11:36 AM


All times are GMT -5. The time now is 04:59 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration