6

I became a professional programmer in the era of object oriented code, and have years of experience programming in C++. I often work on large projects that have years of legacy code in a mix of c++ and c. I feel less comfortable working on pure c parts of systems. From programming in C++ I understand all the c syntax, but there's a hole in my knowledge about how to organise a complex c program without objects, and what constitutes best practise for managing memory that I would like to fill. I learnt c++ after working as a java programmer, and think a bit more c would make me a better c++ programmer, and a bit less of a java translated into c++ programmer

compound eye
  • 1,898
  • 18
  • 23
  • ever tutorial i have found seems to assume you are a c programmer moving to c++. Surely I can't be the only object oriented programmer wanting to understand c better. – compound eye Aug 03 '09 at 00:09
  • I think if you write a couple mid-sized C programs that will teach you how to stay out of trouble. Actually getting into trouble (in any language) is a valuable way to learn. Like touching something hot. – Nosredna Aug 03 '09 at 00:10
  • There are several question in this vein on StackOverflow already (and some c# moving to c questions). From my native c speaker mostly functional in c++ vantage point, I though I saw several things pieces of advice that looked good. They might be worth hunting down. – dmckee --- ex-moderator kitten Aug 03 '09 at 00:29
  • Here are a couple: http://stackoverflow.com/questions/436446/what-is-the-best-way-to-plan-and-organize-development-of-an-application-in-c and http://stackoverflow.com/questions/674722/struggling-with-c-coming-from-object-oriented-land . I think there are others as well. – dmckee --- ex-moderator kitten Aug 03 '09 at 00:34

6 Answers6

1

I was in basically the same boat as you (albeit with less experience, and I started with Python rather than Java), and what worked best for me was sitting down and reading Kernighan and Ritchie. You'll be able to skim the first half of the book since you're comfortable with the syntax, but you'll definitely walk away with a better understanding of low-level memory management.

Reading the parts of the Linux kernel source that have to do with memory management also helps but is not for the faint of heart or the easily bored.

Meredith L. Patterson
  • 4,853
  • 29
  • 30
1

some google results:

C for C++ programmers

C for C++ programmers 2

Gordon Gustafson
  • 40,133
  • 25
  • 115
  • 157
1

In terms of organization, looking at the POSIX APIs, especially pthreads will give you a good idea of how to organize C code. The basic rules of good C project organization are:

  • Don't expose your structures. Use opaque types only.
  • Use the library and data type names as prefixes for function names.
  • Provide "create" and "destroy" functions for allocation/construction and destruction/deallocation.
  • Pass in the opaque type as the first parameter to functions operating on that type.
  • Implement the C APIs using either C or C++ (it's up to you). Obviously, use non-opaque types there.
Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
1

Expanding on another answer, one way is to just write object-oriented C. This is the way most libraries I interact with behave (for example the GNOME stack is almost entirely object-oriented C, mostly based on the gobject library to make it easier). You can think of it as C++ without some of the syntactic sugar.

You end up with an API like


/* in project NewStuff; namespace is ns */
ns_foo_t *ns_foo_new ();
void ns_foo_delete (ns_foo_t *);

int ns_foo_make_waffles (ns_foo_t *this, int no_of_guests);

int main () {
  ns_foo_t *my_foo = ns_foo_new ();
  ns_foo_make_waffles (my_foo, 1);
  ns_foo_delete (my_foo);
}

which corresponds almost exactly to


class Foo {
  public:
    Foo () { /* whatever */ }

    int make_waffles (int no_of_guests) {}
}; 

int main () {
  Foo *my_foo = new Foo ();
  my_foo->make_waffles (1);
  delete my_foo;
}
Community
  • 1
  • 1
RAOF
  • 1,126
  • 9
  • 14
1

You could also check out The C Programming Language ANSI C Edition written by Kernighan and Ritchie. Yes, it's old, but it only clocks in at approximately 200 pages and covers the entire language as well as the standard library.

FloppyDisk
  • 1,693
  • 16
  • 25
0

Looking into building programs with extensive use of pointers as well as low-level structs would be a good start in my opinion anyway. It also depends on what is the program built for. Are you trying to write C code on a embedded rtos board or microcontroller system?

stanigator
  • 10,768
  • 34
  • 94
  • 129
  • i sometimes work on legacy systems which have a lot of c, so that's the initial reason. I also just suspect that it would make me a better c++ programmer to be more familiar with designing programs which are built around data in structs, and other c techniques, rather than assuming that everything needs to be an object. – compound eye Aug 03 '09 at 00:17
  • In that case, I would highly suggest improving your pointer knowledge, as Java doesn't allow you to deal with pointers. Also if you want to learn about hardware optimization, it would be great if you can get an embedded single board computer, microcontrollers, or smartphone/PDA to learn how to write C/C++ optimized for hardware. – stanigator Aug 03 '09 at 00:29