Learning C by reading

S

Sergi Pasoev

Hello

What are the programs you think make a good reading to learn the
language by studying and modifying the source code ? I often try to
understand the things in the standard library, and apart from doing this
kind of search myself, I would be glad to hear some ideas from more
experienced programmers. I think there must be many Free Software
programs which have some well-written, easy to understand code.

Or any good place to search for some code to learn from.
 
J

James Kuyper

Hello

What are the programs you think make a good reading to learn the
language by studying and modifying the source code ? I often try to
understand the things in the standard library, and apart from doing this
kind of search myself, I would be glad to hear some ideas from more
experienced programmers. I think there must be many Free Software
programs which have some well-written, easy to understand code.

Or any good place to search for some code to learn from.

Standard library routines are often a dangerous place to learn C.
There's a couple of reasons for this.

One is that many of the routines are in the standard library precisely
because they cannot be efficiently implemented on all architectures
using portable C code. Therefore, the actual implementation of those
routines often must make heavy use of implementation-specific extensions
to C, or code that is specific to one particular platform; the same
implementor may implement the standard library differently on different
platforms. You can develop a bad habit of writing unnecessarily
unportable code, if you use standard library code as a model.

Secondly, the standard partitions the set of identifiers into several
parts. Many specific identifiers have meanings specified by the C
standard library; these are reserved in many contexts. Keywords fit the
specification of identifiers, but are, in effect, always reserved for
all uses except as macro names. Of the remaining identifiers, some are
reserved for use by the implementation in various contexts. User code
can define these identifiers only in contexts other than the ones where
they are reserved (some of those identifiers are reserved in ALL
contexts). User code can also define any identifier that's not reserved.

This means that implementations of the C standard library are not only
allowed to define identifiers in contexts where they are reserved to the
implementation, but that in many contexts they MUST define them, to
avoid interfering with the rights of users to use identifiers that
aren't reserved, or reserved identifiers in contexts where they're not
reserved. I've seen way too many people get into the habit of putting
'_' at the beginning of lots of their identifiers, because that's what
they saw in the C standard library. They didn't realize that they're not
allowed to use such identifiers, precisely because the standard library
must use such identifiers. You can actually use such identifiers in some
contexts; but it's safer to treat any identifier that starts with an '_'
to as reserved.

In particular, many people mistakenly think that a leading '_' is part
of the header guard idiom, because they see it in the C standard
headers, and copy it in their own headers:

// The standard library must use leading underscores like this:
#ifndef _STDIO_H
#define _STDIO_H
// body of stdio.h
#endif

// You cannot use leading underscores like this:
#ifdef _MYHEADER_H
#define _MYHEADER_H
// body of myheader.h
#endif
 
J

Johann Klammer

Sergi said:
Hello

What are the programs you think make a good reading to learn the
language by studying and modifying the source code ? I often try to
understand the things in the standard library, and apart from doing this
kind of search myself, I would be glad to hear some ideas from more
experienced programmers. I think there must be many Free Software
programs which have some well-written, easy to understand code.

Or any good place to search for some code to learn from.

dmalloc is some nice reading. Also the linux kernel and how they
implement linked lists.

For snippets to do specific basic things rosettacode seems to be where
people go.
 
A

Andrew Cooper

// You cannot use leading underscores like this:
#ifdef _MYHEADER_H
#define _MYHEADER_H
// body of myheader.h
#endif


Incorrect. You shouldn't, but you most definitely can. As is the way
with a large amount of C.

Also, that should be an #ifndef, else you will never include the body of
myheader.h unless you explicitly define _MYHEADER_H somewhere else.

~Andrew
 
J

James Kuyper

Incorrect. You shouldn't, but you most definitely can. As is the way
with a large amount of C.

OK - "You cannot ... with defined behavior."
Also, that should be an #ifndef, else you will never include the body of
myheader.h unless you explicitly define _MYHEADER_H somewhere else.

I need a vacation. What I'm getting in about a month is 6000km road
trip. I'm not sure that qualifies. :-}
 
R

Rui Maciel

Sergi said:
Or any good place to search for some code to learn from.

You can search public source code repositories for C projects. Sourceforge
comes to mind. It hosts a good number of popular FLOSS applications, some
of which may catch your eye.


Rui Maciel
 
N

none

What are the programs you think make a good reading to learn the
language by studying and modifying the source code ? I often try to
understand the things in the standard library, and apart from doing this
kind of search myself, I would be glad to hear some ideas from more
experienced programmers. I think there must be many Free Software
programs which have some well-written, easy to understand code.

Or any good place to search for some code to learn from.

I recommend the book "C Interfaces and Implementations" by
David R. Hanson. It's certainly not a beginner's book, but
if you have a few years of experience with C, you can use
that book to learn much more.
 
P

Phil Carmody

Sergi Pasoev said:
Hello

What are the programs you think make a good reading to learn the
language by studying and modifying the source code ? I often try to
understand the things in the standard library, and apart from doing this
kind of search myself, I would be glad to hear some ideas from more
experienced programmers. I think there must be many Free Software
programs which have some well-written, easy to understand code.

Or any good place to search for some code to learn from.

I would say 'busybox'.

Basically, you've got the source code to a hundred useful programs,
but each one of them has been stripped down to the bare essentials.

Compare the length of the source code for GNU 'true' with busybox's
'true'. Ditto 'false'. Ditto 'yes'. Etc. etc. ...

Don't think busybox's programs are functional enough? Great - that's
your programming exercise!

Unfortunately, the maintainers of busybox do include hackish
microoptimisations simply because they make the x86 binaries
shrink, with no care for the portability of the optimisation.
(I've not checked, maybe it's because all other architectures
are carp, that's a common excuse.)

Phil
--
I'd argue that there is much evidence for the existence of a God.
Pics or it didn't happen.
-- Tom (/. uid 822)
 

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

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top