macro object for C

MOC is the abbreviation of Macro object C. It consists of several macros and a set of functions, it is a pure source level technology that enables efficient object oriented programming in pure C language without any additional compiler or preprocessor. The goal of MOC is to make C more expressive by introducing some mordern language infrastructure. And MOC is an open source project that gives a full play to 'pure source level technology'.

MOC supports
1. Object and class
2. Interface
3. Message
4. Exception handling
5. Dynamic class
6. Extensible class
8. Garbage collection
9. Persistent

MOC is suitable for building sophisticate software or being used as a runtime back end for other programming language. And it doesn't depend on any platform special features. The only assumption is that an ANSI C compliant compiler exists. The basic MOC library consists of 7 source files and 8 header files. So it's easy to build them under a particular platform (including Linux, FreeBSD, Windows etc).

MOC introduces OOP idiom by injecting a type system into C. MOC involves a single inheritance hierarchy for the type system. Every class should derive from obj_t (the root class in MOC) then MOC provides clear semantic to cooperate with class and object.

obj_t defines the default behaviors which determines how to create an object, how to initialize an object, how to destroy an object etc. And all of these behaviors are easy to customize by standard overriding mechanism. In MOC, interface and message are two mechanisms that facilitate inheritance and override. Interface call is resolved at compile time whereas message need runtime resolution.

Besides object representation. Exception handling is another big part of MOC. It has similar semantic and syntax with other popular modern programming language. Exception handling supports TRY CATCH and FINALLY clauses. MOC uses C standard library function setjmp and longjmp to implement exception handling. So it is inefficient on some architecture on which setjmp has to save a large context. But this is a tradeoff between efficiency and introducing a new compiler or preprocessor. I think additional tools will complicate the design and injure the portability of MOC so it is conflict with the goal of MOC.

Garbage collection and persistent are two service that based on MOC object modal. They are useful programming infrastructures. They are software services lay on the top of MOC I provide them to illustrate some portantial usage of MOC.

The latest release of MOC has three parts.
1. moc
2. services
3. scheme interpreter
moc is the basic lib of MOC infrastructure; services implements gargabe collection and persistent; scheme interpreter is a complete sample program that based on moc and services. Scheme is a member of lisp family. MOC scheme is a good sample that illustrates how to apply object oriented paradigm in a interpreter.

If you have any question please read the document first and the author of MOC can be contact at


There is a good book that describes another approach to implement OOP in ANSI-C. The book could be accessed freely via web. The book will help you understand MOC technology. The main difference between MOC and OOC(described by the book) is MOC utilizes C preprocessor to force coding standard while OOC involves an independent preprocessor and MOC provides more features like transparent object persistent etc.

Web site

There is also a
web site that contains some links to OOC technologies. You may reach it by following the link.
Search WWW Search