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