Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #22921 > unrolled thread

Learning C by reading

Started bySergi Pasoev <s.pasoev@gmail.com>
First post2012-06-28 14:13 +0400
Last post2012-07-06 01:15 +0300
Articles 9 — 8 participants

Back to article view | Back to comp.lang.c


Contents

  Learning C by reading Sergi Pasoev <s.pasoev@gmail.com> - 2012-06-28 14:13 +0400
    Re: Learning C by reading James Kuyper <jameskuyper@verizon.net> - 2012-06-28 06:59 -0400
      Re: Learning C by reading Andrew Cooper <amc96@cam.ac.uk> - 2012-06-29 00:56 +0100
        Re: Learning C by reading James Kuyper <jameskuyper@verizon.net> - 2012-06-28 20:21 -0400
    Re: Learning C by reading Johann Klammer <klammerj@NOSPAM.a1.net> - 2012-06-28 18:08 +0200
    Re: Learning C by reading Aaron W. Hsu <arcfide@sacrideo.us> - 2012-06-28 15:00 -0500
    Re: Learning C by reading Rui Maciel <rui.maciel@gmail.com> - 2012-06-29 02:48 +0100
    Re: Learning C by reading rouben@shady.(none) (Rouben Rostamian) - 2012-06-29 08:55 +0000
    Re: Learning C by reading Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-07-06 01:15 +0300

#22921 — Learning C by reading

FromSergi Pasoev <s.pasoev@gmail.com>
Date2012-06-28 14:13 +0400
SubjectLearning C by reading
Message-ID<87bok3u4lj.fsf@gmail.com>
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.

[toc] | [next] | [standalone]


#22923

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-06-28 06:59 -0400
Message-ID<jshdet$s4u$1@dont-email.me>
In reply to#22921
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

[toc] | [prev] | [next] | [standalone]


#22956

FromAndrew Cooper <amc96@cam.ac.uk>
Date2012-06-29 00:56 +0100
Message-ID<Va6Hr.395935$ME5.247721@fx22.am4>
In reply to#22923
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

[toc] | [prev] | [next] | [standalone]


#22958

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-06-28 20:21 -0400
Message-ID<4FECF508.7070207@verizon.net>
In reply to#22956
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. :-}

[toc] | [prev] | [next] | [standalone]


#22934

FromJohann Klammer <klammerj@NOSPAM.a1.net>
Date2012-06-28 18:08 +0200
Message-ID<4fec8171$0$1576$91cee783@newsreader04.highway.telekom.at>
In reply to#22921
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.

[toc] | [prev] | [next] | [standalone]


#22948

FromAaron W. Hsu <arcfide@sacrideo.us>
Date2012-06-28 15:00 -0500
Message-ID<T9-dnRYy_99BKnHSnZ2dnUVZ_sSdnZ2d@giganews.com>
In reply to#22921
Sergi Pasoev <s.pasoev@gmail.com> writes:

>What are the programs you think make a good reading to learn the
>language by studying and modifying the source code ?

For myself, one of the more enjoyable reads, and relatively small 
programs has been the Stanford GraphBase (sgb), which has a lot 
of programs and functions in it that were specifically meant to be 
read. 

Stanford GraphBase: http://www-cs-faculty.stanford.edu/~knuth/sgb.html
CWEB: http://www-cs-faculty.stanford.edu/~knuth/cweb.html

They come in printed and electronic form.

-- 
Aaron W. Hsu | arcfide@sacrideo.us | http://www.sacrideo.us
Programming is just another word for the lost art of thinking.

[toc] | [prev] | [next] | [standalone]


#22959

FromRui Maciel <rui.maciel@gmail.com>
Date2012-06-29 02:48 +0100
Message-ID<jsj1gq$uiu$1@speranza.aioe.org>
In reply to#22921
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

[toc] | [prev] | [next] | [standalone]


#22960

Fromrouben@shady.(none) (Rouben Rostamian)
Date2012-06-29 08:55 +0000
Message-ID<jsjqin$bm0$1@news.albasani.net>
In reply to#22921
In article <87bok3u4lj.fsf@gmail.com>,
Sergi Pasoev  <s.pasoev@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#23097

FromPhil Carmody <thefatphil_demunged@yahoo.co.uk>
Date2012-07-06 01:15 +0300
Message-ID<87d34926sy.fsf@bazspaz.fatphil.org>
In reply to#22921
Sergi Pasoev <s.pasoev@gmail.com> 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)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web