Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Learning C by reading

Reply
Thread Tools

Learning C by reading

 
 
Sergi Pasoev
Guest
Posts: n/a
 
      06-28-2012
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.
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      06-28-2012
On 06/28/2012 06:13 AM, Sergi Pasoev wrote:
> 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
--
James Kuyper
 
Reply With Quote
 
 
 
 
Johann Klammer
Guest
Posts: n/a
 
      06-28-2012
Sergi Pasoev wrote:
> 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.
 
Reply With Quote
 
Andrew Cooper
Guest
Posts: n/a
 
      06-28-2012
On 28/06/2012 11:59, James Kuyper wrote:
>
> // 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
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      06-29-2012
On 06/28/2012 07:56 PM, Andrew Cooper wrote:
> On 28/06/2012 11:59, James Kuyper wrote:
>>
>> // 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.


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. :-}


 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      06-29-2012
Sergi Pasoev wrote:

> 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
 
Reply With Quote
 
none
Guest
Posts: n/a
 
      06-29-2012
In article <(E-Mail Removed)>,
Sergi Pasoev <(E-Mail Removed)> wrote:
>
>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.

--
Rouben Rostamian
 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      07-05-2012
Sergi Pasoev <(E-Mail Removed)> writes:
> 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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Learning C by Reading Sergi Pasoev C Programming 0 06-29-2012 01:17 PM
Learning python by reading Sergi Pasoev Python 0 06-28-2012 07:38 AM
[I'm learning C]: Learning to use ucontext Andrey Popp C Programming 5 01-31-2012 01:05 AM
Learning C and Learning Make/Configure/Building/Linking Hal Vaughan C Programming 7 03-21-2006 05:07 PM
e-learning, (collaborative learning environment) collinm Java 1 09-08-2005 09:52 PM



Advertisments