learning C with a C++ background

Discussion in 'C Programming' started by jonathanmcdougall@gmail.com, Jun 14, 2006.

  1. Guest

    I just got a new job yesterday where most of the programming is done in
    C. Although I have several years of experience in C++ and other
    object-oriented languages, I have next to none in C.

    What I am looking for is a book (or some other resource) on "how to
    learn C if you already know C++" or, more exactly, "how to program in a
    procedural language when your only experience is with object-oriented
    languages".

    I find myself wanting to emulate many OO features and I have the
    impression this is wrong. I need to know how source files are
    organized, I'm lost without namespaces, classes and the standard
    library (containers, someone?), how do you cope with all these variable
    definitions at the top of a function, do you really create
    as-big-as-you-can arrays of chars for strings, etc. etc.

    Pretty much everything I was told and teached to avoid in C++ seem to
    be needed in C. That's counter-intuitive for me and hard to get over.
    Is there a book of wisdom somewhere that could help me understand
    idioms, design patterns or oft-used construction?

    Thank you,


    Jonathan
    , Jun 14, 2006
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    > I just got a new job yesterday where most of the programming is done in
    > C. Although I have several years of experience in C++ and other
    > object-oriented languages, I have next to none in C.
    >

    You poor bugger!

    > What I am looking for is a book (or some other resource) on "how to
    > learn C if you already know C++" or, more exactly, "how to program in a
    > procedural language when your only experience is with object-oriented
    > languages".
    >

    Just read K&R, with an open mind.

    > I find myself wanting to emulate many OO features and I have the
    > impression this is wrong.


    It's not wrong, it might not be idiomatic C, but you can write good,
    clean OO code in C.

    > I need to know how source files are
    > organized, I'm lost without namespaces, classes and the standard
    > library (containers, someone?), how do you cope with all these variable
    > definitions at the top of a function, do you really create
    > as-big-as-you-can arrays of chars for strings, etc. etc.
    >

    File organisation isn't much different form C++, except your headers
    define a functional interface rather than an OO one.

    If you can use C99, variable declaration rules are the same as C++. If
    you follow good programming practice and use short methods, the
    traditional C way isn't a big deal.

    > Pretty much everything I was told and teached to avoid in C++ seem to
    > be needed in C. That's counter-intuitive for me and hard to get over.
    > Is there a book of wisdom somewhere that could help me understand
    > idioms, design patterns or oft-used construction?
    >

    Study a good C code base.

    --
    Ian Collins.
    Ian Collins, Jun 14, 2006
    #2
    1. Advertising

  3. Ian Collins wrote:
    > wrote:
    > > I just got a new job yesterday where most of the programming is done in
    > > C. Although I have several years of experience in C++ and other
    > > object-oriented languages, I have next to none in C.
    > >

    > You poor bugger!


    :) Thanks

    > > What I am looking for is a book (or some other resource) on "how to
    > > learn C if you already know C++" or, more exactly, "how to program in a
    > > procedural language when your only experience is with object-oriented
    > > languages".
    > >

    > Just read K&R, with an open mind.


    Got that.

    > > I find myself wanting to emulate many OO features and I have the
    > > impression this is wrong.

    >
    > It's not wrong, it might not be idiomatic C, but you can write good,
    > clean OO code in C.


    I find the Gtk library to be a bit cumbersome, for example, as if it
    wasn't C nor C++. I might be wrong here, but that's the impression I
    have.

    > If you can use C99, variable declaration rules are the same as C++.


    I can't. Funny, I spent twenty minutes today trying to understand a
    "invalid identifier before 'variable_name'" before remembering that
    variables must be declared on top of a function.

    > > Pretty much everything I was told and teached to avoid in C++ seem to
    > > be needed in C. That's counter-intuitive for me and hard to get over.
    > > Is there a book of wisdom somewhere that could help me understand
    > > idioms, design patterns or oft-used construction?
    > >

    > Study a good C code base.


    I will, but I am not in a good position of knowing whether some C code
    is "good" or not.


    Jonathan
    Jonathan Mcdougall, Jun 14, 2006
    #3
  4. Ian Collins Guest

    Ian Collins, Jun 14, 2006
    #4
  5. Ian Collins Guest

    Jonathan Mcdougall wrote:
    > Ian Collins wrote:
    >
    >> wrote:
    >>
    >>>I find myself wanting to emulate many OO features and I have the
    >>>impression this is wrong.

    >>
    >>It's not wrong, it might not be idiomatic C, but you can write good,
    >>clean OO code in C.

    >
    >
    > I find the Gtk library to be a bit cumbersome, for example, as if it
    > wasn't C nor C++. I might be wrong here, but that's the impression I
    > have.
    >

    Have a look at XView, it may be rather old, but it's fairly clean OO C
    code. I first learned about OO by using it many years ago.

    --
    Ian Collins.
    Ian Collins, Jun 14, 2006
    #5
  6. On 2006-06-14, <> wrote:
    > I just got a new job yesterday where most of the programming is done in
    > C. Although I have several years of experience in C++ and other
    > object-oriented languages, I have next to none in C.
    >

    Ooh. That's one of my main reasons for avoiding C++ (other than to make
    sure I can still write it if I need to).

    > What I am looking for is a book (or some other resource) on "how to
    > learn C if you already know C++" or, more exactly, "how to program in a
    > procedural language when your only experience is with object-oriented
    > languages".
    >

    This newsgroup is probably the best source I can think of. K&R is
    recommended all willy-nilly here, and I'd start with that book. C
    Unleashed is another well-written textbook.

    > I find myself wanting to emulate many OO features and I have the
    > impression this is wrong. I need to know how source files are
    > organized, I'm lost without namespaces, classes and the standard
    > library (containers, someone?)


    Using structs and explicit this pointers in functions, you can emulate
    classes. Namespaces are fairly easy to get along without. C has a
    standard library that works great. Well-written header files will be
    easy to understand (I personally find them moreso than C++ headers,
    because they lack extra keywords for namespaces and the like).

    > , how do you cope with all these variable
    > definitions at the top of a function,


    It's much easier. If you need to find where a variable is declared, you
    know exactly where to look. Also, you learn to use less variables that
    way (hopefully as a side effect of writing more efficient code). For temp
    variables you always have block scope.

    > do you really create
    > as-big-as-you-can arrays of chars for strings, etc. etc.
    >


    No. We do strings much the same as in C++, but our functions aren't part
    of a string class. Things tend to be easier to understand that way.

    In C99 you have VLA's, but personally those look tricky to maintain to me.

    > Pretty much everything I was told and teached to avoid in C++ seem to
    > be needed in C. That's counter-intuitive for me and hard to get over.
    > Is there a book of wisdom somewhere that could help me understand
    > idioms, design patterns or oft-used construction?
    >


    Conversely, everything that C++ textbooks say not to do make me wonder what
    Bjarne was thinking when he made that crazy language. Basically, you'll get
    used to it pretty quickly.

    --
    Andrew Poelstra < http://www.wpsoftware.net/blog >
    To email me, use "apoelstra" at the above address.
    I know that area of town like the back of my head.
    Andrew Poelstra, Jun 14, 2006
    #6
  7. Andrew Poelstra wrote:
    > On 2006-06-14, <> wrote:


    > > What I am looking for is a book (or some other resource) on "how to
    > > learn C if you already know C++" or, more exactly, "how to program in a
    > > procedural language when your only experience is with object-oriented
    > > languages".
    > >

    > This newsgroup is probably the best source I can think of. K&R is
    > recommended all willy-nilly here, and I'd start with that book. C
    > Unleashed is another well-written textbook.


    K&R and C Unleashed, noted. Thanks.

    > > I find myself wanting to emulate many OO features and I have the
    > > impression this is wrong. I need to know how source files are
    > > organized, I'm lost without namespaces, classes and the standard
    > > library (containers, someone?)

    >
    > Using structs and explicit this pointers in functions, you can emulate
    > classes.


    But is this what I want to do? Do I want to emulate classes in C?

    > C has a
    > standard library that works great.


    But I find it lacks important several features. I searched a while for
    an "STL" equivalent (containers and algorithms). What do people use
    when they need a linked list? Are there commonly used libraries
    available, such as boost for C++?

    > Conversely, everything that C++ textbooks say not to do make me wonder what
    > Bjarne was thinking when he made that crazy language. Basically, you'll get
    > used to it pretty quickly.


    Just to specify here: I am hoping this thread won't become a religious
    war. Let's try to be... uhmm.. neutral :)


    Jonathan
    Jonathan Mcdougall, Jun 14, 2006
    #7
  8. Ian Collins Guest

    Jonathan Mcdougall wrote:
    >>
    >>Using structs and explicit this pointers in functions, you can emulate
    >>classes.

    >
    >
    > But is this what I want to do? Do I want to emulate classes in C?
    >

    Only you can answer that! If that you are happy with that style, use it
    (I do).

    >
    >>C has a
    >>standard library that works great.

    >
    >
    > But I find it lacks important several features. I searched a while for
    > an "STL" equivalent (containers and algorithms). What do people use
    > when they need a linked list? Are there commonly used libraries
    > available, such as boost for C++?
    >

    Have a look back in this group's archives for a thread "Boost process
    and C", it will give you an idea...

    --
    Ian Collins.
    Ian Collins, Jun 14, 2006
    #8
  9. CBFalconer Guest

    Jonathan Mcdougall wrote:
    > Ian Collins wrote:
    >

    .... snip ...
    >>
    >> Study a good C code base.

    >
    > I will, but I am not in a good position of knowing whether some C
    > code is "good" or not.


    What you find in K&R, or "The Practice of Programming", or "C
    Unleashed", or in my releases
    <http://cbfalconer.home.att.net/download/> are all good. Some may
    quibble at the last.

    In particular my hashlib is written with an OO attitude.

    --
    Some informative links:
    news:news.announce.newusers
    http://www.geocities.com/nnqweb/
    http://www.catb.org/~esr/faqs/smart-questions.html
    http://www.caliburn.nl/topposting.html
    http://www.netmeister.org/news/learn2quote.html
    CBFalconer, Jun 14, 2006
    #9
  10. "Jonathan Mcdougall" <> writes:

    >> C has a
    >> standard library that works great.

    >
    > But I find it lacks important several features. I searched a while for
    > an "STL" equivalent (containers and algorithms). What do people use
    > when they need a linked list?

    I'd go for glib http://developer.gnome.org/doc/API/2.0/glib/index.html
    but that's off-topic here I guess.

    >Are there commonly used libraries
    > available, such as boost for C++?

    I'd argue this depends on the operating system you are using.

    Regards
    Friedrich

    --
    Please remove just-for-news- to reply via e-mail.
    Friedrich Dominicus, Jun 14, 2006
    #10
  11. On Tue, 13 Jun 2006 19:41:15 -0700, Jonathan Mcdougall wrote:
    >> Using structs and explicit this pointers in functions, you can emulate
    >> classes.

    >
    > But is this what I want to do? Do I want to emulate classes in C?


    Well, not necessarily for polymorphism. But starting from the premise that
    globals are bad, any interdependent set of variables should co-exist
    within an allocated structure object (object in the C sense), and
    operations on that set should occur via routines which take as an argument
    a pointer to that structure. Whether you contract to make member variables
    of that structure opaque, or even go further and employ a member table of
    function pointers with which to operate on that structure is up to you.
    Typically you don't need to go that far.

    >> C has a
    >> standard library that works great.

    >
    > But I find it lacks important several features. I searched a while for an
    > "STL" equivalent (containers and algorithms). What do people use when they
    > need a linked list? Are there commonly used libraries available, such as
    > boost for C++?


    Goto a BSD respository (OpenBSD, FreeBSD, NetBSD, etc) and pull down
    sys/queue.h and sys/tree.h. You'd be hard-pressed to find better list or
    tree implementations (for most qualities one can come up with). And
    they're type-safe to boot, just like STL.

    For hash tables CBFalconer's implementation is always available :)

    - Bill
    William Ahern, Jun 14, 2006
    #11
  12. Chris Torek Guest

    In article <>
    Jonathan Mcdougall <> wrote:
    >... Do I want to emulate classes in C?


    Sometimes. The cost (in terms of complicating-up the source) can
    be high, so one should do it only when the benefit is high too.

    >But I find [the Standard C Library] lacks important several features.
    >I searched a while for an "STL" equivalent (containers and algorithms).
    >What do people use when they need a linked list?


    Personally, I find linked lists so trivial that I would rather
    open-code most of them -- there is nothing difficult about:

    for (p = head; p != NULL; p = p->next)
    ... operate on p ...

    and even ordered list insertion is pretty easy:

    for (pp = &head; (p = *pp) != NULL; pp = &p->next)
    if (item goes here)
    break;
    *pp = new;
    new->next = p;

    -- but if the lists get more complicated, or in projects that
    have to be maintained by people who find the above insertion code
    puzzling :) , I tend to use the BSD <sys/queue.h> macros.
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
    Chris Torek, Jun 14, 2006
    #12
  13. Bill Pursell Guest

    Ian Collins wrote:
    > Jonathan Mcdougall wrote:
    > > Ian Collins wrote:
    > >>
    > >>Study a good C code base.

    > >
    > >
    > > I will, but I am not in a good position of knowing whether some C code
    > > is "good" or not.
    > >

    > Have a look at some of the newer code in OpenSolaris, something like ZFS:
    >
    > http://cvs.opensolaris.org/source/xref/on/usr/src/lib/libzfs/common/



    I'd love to see some of the regular's opinions on common code
    that is good as well as bad. eg, a short list of well written code
    and a (shorter!) list of code that is written poorly. (Although that
    might generate a lot more heat.)
    Bill Pursell, Jun 14, 2006
    #13
  14. Ian Collins Guest

    Bill Pursell wrote:
    >
    > I'd love to see some of the regular's opinions on common code
    > that is good as well as bad. eg, a short list of well written code
    > and a (shorter!) list of code that is written poorly. (Although that
    > might generate a lot more heat.)
    >

    I think we had a good example of the latter from GNU last week :)

    --
    Ian Collins.
    Ian Collins, Jun 14, 2006
    #14
  15. On 2006-06-14, Jonathan Mcdougall <> wrote:
    > Andrew Poelstra wrote:
    >> On 2006-06-14, <> wrote:

    >
    >> > What I am looking for is a book (or some other resource) on "how to
    >> > learn C if you already know C++" or, more exactly, "how to program in a
    >> > procedural language when your only experience is with object-oriented
    >> > languages".
    >> >

    >> This newsgroup is probably the best source I can think of. K&R is
    >> recommended all willy-nilly here, and I'd start with that book. C
    >> Unleashed is another well-written textbook.

    >
    > K&R and C Unleashed, noted. Thanks.
    >

    That's "The C Programming Language" by Kernighan and Ritchie. But it
    looks like you're bright enough to use Google, so I didn't need to say
    that. ;-)

    >> > I find myself wanting to emulate many OO features and I have the
    >> > impression this is wrong. I need to know how source files are
    >> > organized, I'm lost without namespaces, classes and the standard
    >> > library (containers, someone?)

    >>
    >> Using structs and explicit this pointers in functions, you can emulate
    >> classes.

    >
    > But is this what I want to do? Do I want to emulate classes in C?
    >

    Oftentimes you will, but emulating classes should be a side effect,
    not a goal. You'll find that you can do a lot of stuff without using
    any complex data structures. (For example, crypto usually requires a
    few primitive arrays and that's it).

    When you use structs, you'll almost always have a few functions that
    go with it.

    >> C has a
    >> standard library that works great.

    >
    > But I find it lacks important several features. I searched a while for
    > an "STL" equivalent (containers and algorithms). What do people use
    > when they need a linked list? Are there commonly used libraries
    > available, such as boost for C++?
    >

    There was talk of making a clc library, but that disintegrated for various
    superficial reasons, if I got the history right.

    For linked lists and such, each programmer usually builds these kind of
    libraries up over the years, until he can write a two-month killer app in
    2 days.

    You can get all the useful stuff (linked lists, trees, etc) off of the C
    Unleashed CD, if you want. It's rather unfortunate that we haven't stand-
    ardized stuff like that.

    >> Conversely, everything that C++ textbooks say not to do make me wonder what
    >> Bjarne was thinking when he made that crazy language. Basically, you'll get
    >> used to it pretty quickly.

    >
    > Just to specify here: I am hoping this thread won't become a religious
    > war. Let's try to be... uhmm.. neutral :)
    >

    I didn't mean 'crazy' in a bad way. Just... different. :)

    --
    Andrew Poelstra < http://www.wpsoftware.net/blog >
    To email me, use "apoelstra" at the above address.
    I know that area of town like the back of my head.
    Andrew Poelstra, Jun 14, 2006
    #15
  16. wrote:
    >
    > I just got a new job yesterday where most of the programming is done in
    > C. Although I have several years of experience in C++ and other
    > object-oriented languages, I have next to none in C.
    >
    > What I am looking for is a book (or some other resource) on "how to
    > learn C if you already know C++" or, more exactly, "how to program in a
    > procedural language when your only experience is with object-oriented
    > languages".
    >
    > I find myself wanting to emulate many OO features and I have the
    > impression this is wrong. I need to know how source files are
    > organized, I'm lost without namespaces, classes and the standard
    > library (containers, someone?), how do you cope with all these variable
    > definitions at the top of a function, do you really create
    > as-big-as-you-can arrays of chars for strings, etc. etc.
    >
    > Pretty much everything I was told and teached to avoid in C++ seem to
    > be needed in C. That's counter-intuitive for me and hard to get over.
    > Is there a book of wisdom somewhere that could help me understand
    > idioms, design patterns or oft-used construction?
    >

    Now you know how C programmers feel who have to learn C++...

    --
    +----------------------------------------------------------------+
    | Charles and Francis Richmond richmond at plano dot net |
    +----------------------------------------------------------------+
    Charles Richmond, Jun 14, 2006
    #16
  17. santosh Guest

    Jonathan Mcdougall wrote:
    > Andrew Poelstra wrote:
    > > On 2006-06-14, <> wrote:

    >
    > > > What I am looking for is a book (or some other resource) on "how to
    > > > learn C if you already know C++" or, more exactly, "how to program in a
    > > > procedural language when your only experience is with object-oriented
    > > > languages".

    > > Using structs and explicit this pointers in functions, you can emulate
    > > classes.

    >
    > But is this what I want to do? Do I want to emulate classes in C?


    Not necessarily. Emulating classes is useful only if your application
    fits well into the OO paradigm. Otherwise functions manipulating
    structures will do most of what you need.

    > > C has a
    > > standard library that works great.

    >
    > But I find it lacks important several features. I searched a while for
    > an "STL" equivalent (containers and algorithms). What do people use
    > when they need a linked list? Are there commonly used libraries
    > available, such as boost for C++?


    There are several popular libraries that provide these useful
    algorithms and data structures. GLib is one example. If none are
    suitable for your application, (either technically or legally), then
    you'll have to implement your own.

    If you implement them in a flexible, and portable manner, then you can
    re-use them whenever you need it.
    santosh, Jun 14, 2006
    #17
  18. Default User Guest

    Jonathan Mcdougall wrote:

    > Andrew Poelstra wrote:


    > > Using structs and explicit this pointers in functions, you can
    > > emulate classes.

    >
    > But is this what I want to do? Do I want to emulate classes in C?


    I'm someone who has worked extensively in both languages. My personal
    opinion is that bending C around to look like C++ is NOT the way to go.
    Two reasons:

    1. C is more suited to procedural programming. It doesn't have a lot of
    the features that simplify OO programming.

    2. Probably no one else there is doing C coding like that. Likely
    you'll get torn apart in code reviews and your supervision will make
    you redo it all anyway. If they wanted C++, they USE C++.





    Brian
    Default User, Jun 14, 2006
    #18
  19. Andrew Poelstra wrote:
    >
    > >> > I find myself wanting to emulate many OO features and I have the
    > >> > impression this is wrong. I need to know how source files are
    > >> > organized, I'm lost without namespaces, classes and the standard
    > >> > library (containers, someone?)
    > >>
    > >> Using structs and explicit this pointers in functions, you can emulate
    > >> classes.

    > >
    > > But is this what I want to do? Do I want to emulate classes in C?
    > >

    > Oftentimes you will, but emulating classes should be a side effect,
    > not a goal.


    That's an interesting statement. Actually, I find it too easy to get
    sloppy, as if all the restrictions I applied to C++ were suddenly gone
    in C. Many of the code I saw use global variables, 100+ line functions,
    bad names and terse statements.

    However, I do feel I am getting somwhere. The next pay is in two weeks,
    so the books will have to wait though :)

    > There was talk of making a clc library, but that disintegrated for various
    > superficial reasons, if I got the history right.


    It's sad, really. A strong standard library is to me very important in
    a language.

    > For linked lists and such, each programmer usually builds these kind of
    > libraries up over the years, until he can write a two-month killer app in
    > 2 days.


    I can't believe, in all thoses years, that the standard comitee wasn't
    able to build a standard library. Are there (this is perhaps off-topic)
    historical reasons for that?


    Jonathan
    Jonathan Mcdougall, Jun 16, 2006
    #19
  20. CBFalconer Guest

    Jonathan Mcdougall wrote:
    > Andrew Poelstra wrote:
    >

    .... snip ...
    >
    > It's sad, really. A strong standard library is to me very
    > important in a language.
    >
    >> For linked lists and such, each programmer usually builds these
    >> kind of libraries up over the years, until he can write a
    >> two-month killer app in 2 days.

    >
    > I can't believe, in all thoses years, that the standard comitee
    > wasn't able to build a standard library. Are there (this is
    > perhaps off-topic) historical reasons for that?


    There is a well-standardized library. See the C-library and C99
    links in my sig. below.

    --
    Some useful references about C:
    <http://www.ungerhu.com/jxh/clc.welcome.txt>
    <http://www.eskimo.com/~scs/C-faq/top.html>
    <http://benpfaff.org/writings/clc/off-topic.html>
    <http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
    <http://www.dinkumware.com/refxc.html> (C-library}
    <http://gcc.gnu.org/onlinedocs/> (GNU docs)
    <http://clc-wiki.net> (C-info)
    CBFalconer, Jun 16, 2006
    #20
    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. mason66
    Replies:
    0
    Views:
    421
    mason66
    Jul 27, 2006
  2. JWL
    Replies:
    4
    Views:
    587
    Michael Winter
    Sep 26, 2006
  3. Hal Vaughan
    Replies:
    7
    Views:
    473
  4. jc
    Replies:
    3
    Views:
    1,728
  5. Andrey Popp

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

    Andrey Popp, Jan 29, 2012, in forum: C Programming
    Replies:
    5
    Views:
    712
    Keith Thompson
    Jan 31, 2012
Loading...

Share This Page