I come from a java background (from my CS classes) and a semester of C++. I am just finishing up a OpenCV project for my Co-Op that\'s in pure C, so I\'m a bit late in aski
You can do object oriented design in pure C. An easy approach is to think of a module as a class
with public methods as ordinary functions that require the this
parameter as an explicit first argument.
It helps if the class
name is a prefix on the function name, and if all private functions and class data are declared static
. You build constructors with malloc()
to get the memory, and explicit initialization of the data fields.
A constructor for an object with entirely private data members can expose an opaque pointer (typed void *
even, or as a pointer to an incomplete type if type safety is desired). If you want to have only public data members, then a pointer to a publicly defined struct
works well.
This pattern is followed by a number of libraries. An initialization function returns a cookie that must be passed back to all library methods, and one method serves as a destructor.
Of course, there are other ways to design well in pure C, but if OO works for you, you don't have to abandon it completely.
Work with other good C programmers. Have a code review with them. Not only let them look at your code, but you look at their code.
You may be interested in checking out the answers to a similar question I asked not too long ago. Moreover, if you're interested in C style guides, you may want to take a look at this page since it is a repository for C (and C++) style guides. If you're in the mood for a good laugh, please take a look at the NASA C Style Guide. In particular, take a look at the massive comment... you'll know which one I'm talking about. Don't write comments like this please.
I personally recommend the Indian Hill C Style Guide with some modifications. Furthermore, you may want to purchase the book C Interfaces and Implementations if you're having trouble designing large-scale programs in C.
If you are interested in critical environment standards, then MISRA C could be of interest to you. I found it helpful.
You can get MISRA C 2004 here.
Short overview of the updated MISRA C 2012 here.
Good news is that you can program in a semi-object oriented fashion in C. You can protect data, expose access functions, etc. It may not have all of the fanciness of C++, but from what I have seen of other people's C++ code, many people do not use the fanciness anyway. In other words, people write C code inside a class, where in C you'd write the same code without the class container.
First, read a book on C programming and style, K&R is fine. Second, I'd recommend checking out CERT Programming Standard. Even though this site is primarily focused on "Secure" coding standards, much of the content here is general code quality standards everyone should follow. Doing the things mentioned here will improved your quality, eliminate pesky bugs, and as a side effect, make your code more secure.
You can make a function or object's scope local to its source file by declaring it "static". That helps a bit.
Otherwise, the typical idiom I see for avoiding namespace clashes is to put some kind of facility identifier onto the name. For instance, everything in foo.c might be named foo_*