Learning C by reading

Discussion in 'C Programming' started by Sergi Pasoev, Jun 28, 2012.

  1. Sergi Pasoev

    Sergi Pasoev Guest

    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.
     
    Sergi Pasoev, Jun 28, 2012
    #1
    1. Advertising

  2. Sergi Pasoev

    James Kuyper Guest

    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
     
    James Kuyper, Jun 28, 2012
    #2
    1. Advertising

  3. 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.
     
    Johann Klammer, Jun 28, 2012
    #3
  4. 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
     
    Andrew Cooper, Jun 29, 2012
    #4
  5. Sergi Pasoev

    James Kuyper Guest

    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. :-}
     
    James Kuyper, Jun 29, 2012
    #5
  6. Sergi Pasoev

    Rui Maciel Guest

    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
     
    Rui Maciel, Jun 29, 2012
    #6
  7. Sergi Pasoev

    none Guest

    In article <>,
    Sergi Pasoev <> 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
     
    none, Jun 29, 2012
    #7
  8. Sergi Pasoev

    Phil Carmody Guest

    Sergi Pasoev <> 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)
     
    Phil Carmody, Jul 5, 2012
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darrel
    Replies:
    3
    Views:
    689
    Kevin Spencer
    Nov 11, 2004
  2. Hal Vaughan
    Replies:
    7
    Views:
    494
  3. Andrey Popp

    [I'm learning C]: Learning to use ucontext

    Andrey Popp, Jan 29, 2012, in forum: C Programming
    Replies:
    5
    Views:
    769
    Keith Thompson
    Jan 31, 2012
  4. Sergi Pasoev

    Learning python by reading

    Sergi Pasoev, Jun 28, 2012, in forum: Python
    Replies:
    0
    Views:
    189
    Sergi Pasoev
    Jun 28, 2012
  5. Sergi Pasoev

    Learning C by Reading

    Sergi Pasoev, Jun 29, 2012, in forum: C Programming
    Replies:
    0
    Views:
    338
    Sergi Pasoev
    Jun 29, 2012
Loading...

Share This Page