I am trying to broaden my understanding of the history and development of object-oriented programming, and I am curious to find out if C was designed to facilitate Object-Oriented programming? (like C++ and Objective-C definitely are) or if it was, on the contrary, simply just a clever exploitation of the language's constructs.

I cannot seem to find any sources, including K&R, where the original authors comment on this approach.

Recently I have been looking into OOP in ANSI-C, which is described in Object-Oriented Programming With ANSI-C by Axel Schreiner.

For a freely available PDF version visit http://www.cs.rit.edu/~ats/books/ooc.pdf.

A slightly different approach is used in the part of the Linux kernel dealing with file systems, visit http://lwn.net/Articles/444910/ for more info.

The common idea is to put function pointers in a struct along with fields in order to 'emulate' a class' methods and data members.


From Wikipedia.org/wiki/Object-oriented_programming#History

Objects as a formal concept in programming were introduced in the 1960s in Simula 67

While Wikipedia.org/wiki/C_programming#K.26R_C states that

In 1978, Brian Kernighan and Dennis Ritchie published the first edition of The C Programming Language.

The chronology seem to suggests that K&R must have been well aware of OOP.

So again:

Was C designed to facilitate Object-Oriented programming?

  • 7
    You had me until the last sentence. All you're really looking for are references to whether or no K&R had awareness of OOP? That you can put pointers into a struct is self-evident; it's part of the language design, and is not undocumented. I have a thick, theoretical book about Object Orientation at home that was written before OO was fully baked, and C predates that book, so I disagree with your assertion that K&R had OO in mind. Apr 29 '13 at 14:30
  • 1
    But perhaps its a question of terms. Objects alone do not constitute object-oriented programming, as any veteran VB6 programmer will attest. Apr 29 '13 at 14:43
  • 1
    OO has been baking for a long time, it would be haphazard to call it done even now; 20 years from now there will be some new form of "OO" with books and languages and developers wondering if C# meant to be OO, or it was a happenstance of the way classes were implemented.. Though I don't suspect K&R had any intent towards OO when they made what was meant to be a portable assembler Apr 29 '13 at 15:09
  • 1
    I was going to downvote, but then noticed the book's author actually says "I had fun discovering that ANSI-C is a full-scale object-oriented language". The author seems to confuse "object-orientation" (which C isn't) with "it's possible to simulate it with C" (which of course C can do, since it can do pretty much everything). A language's "full-scale" style/paradigm is not only about what it enables, but also about what it restricts, and C doesn't have many restrictions. You might as well call C "a full-scale functional language", since you can emulate functional programs with it.
    – Andres F.
    Aug 7 '13 at 14:58

Short version: C was before OOP's time, so it wasn't a design choice.

Object oriented programming, OOP, is an old idea. But as a cultural phenomenon it only took off in the 80's and 90's. As someone born in the 80's I'd say it was C++ that launched the hype, set the trend, started the buzzword, or otherwise got OOP off the ground. That turned it from an academic pet project to something businesses looked for on resumes. And that's the sort of statement out of ignorance that will get the grey-beards to come out of the woodwork claiming that it was really Delphi or some other language that was the igniter. And they'll be countered by the historians that can show some Renaissance Italian first had the concept of oriented objects. And the whole thing ends in flames. But trust me, OOP really took off well after C was designed.

History aside, C is a low-level language and as such it can implement nearly anything that the higher level languages take for granted. You can implement a system that acts like C++'s inheritance. You can implement a system that acts like Java's reflection. Whatever crazy design patterns these young whippersnappers come up with in their fancy dancy new languages will probably be able to be implemented in C. Whether or not it's a good idea, is another issue.

  • 3
    AFAIK, SmallTalk has the best claim to most accepted OO language prior to C++. But my beard isn't grey enough to really weigh in on that discourse.
    – user53019
    Apr 29 '13 at 15:55
  • 3
    Thank you for your answer. But as a cultural phenomenon it only took off in the 80's and 90's. was really what I was looking for.
    – Einar
    Apr 30 '13 at 15:23
  • 2
    As far as I can remember OOP took off with graphical user interfaces (starting mid 80's) because it turned out to be very practical to design GUI's in an object-oriented way (with OOP it can be easier to organize code than with a procedural approach). AFAIK GUI development was also one of the motivations behind Smalltalk. Mainstream languages like C++, objective C, Pascal with objects (like Turbo Pascal), made OOP popular. When C was born, OOP existed already but had not gotten so much attention. C was rather born as a simple, low level language for writing operating systems.
    – Giorgio
    Aug 7 '13 at 12:29
  • C++ was what popularized Object Oriented programming. In those days, there was a sense that there was a need for a new paradigm and it seemed like the direction would be something like Modula 2 or Ada. C++ (and Object-Oriented programming) seemed to come out of the blue. Because C++ (and Objective C) did not require you to start from scratch, they were a lot easier for working programmers to approach. Earlier systems seemed like academic exercises with little connection to "real work". Aug 8 '13 at 1:42
  • No love for Object Pascal, in 1985/86? Apple didn't drop Object Pascal and move to C++ until they switched from Motorola 68K CPU's to IBM PowerPC CPU's in '94. And of course there was Borland's Object Pascal (which became Turbo Pascal), first on the Mac in '86 then on DOS in '89. Seems I remember the Windows API still being very much a C API using the PASCAL calling convention in those days. And as someone else hinted, without Smalltalk (1969 to 1972), there'd be no C++, really... Apr 3 '14 at 22:14

While the idea of OOP existed prior to the development of C, with Simula predating C by 5 years or so, none of C's language constructs actively encourage and facilitate OOP. This doesn't mean that you can't do OOP, you just have to create it yourself. Two examples that immediately come to mind are early C++ compilers that output C code & the Gnome project's extensive use of GObject to bring objects to C developers.

  • I remember taking a 4-day course in 1990 on how to build a full-blown C++ interpreter using just C macros. Aug 7 '13 at 22:19
  • @PieterGeerkens: It should be noted that it was 8 years before C++ was standardized and had fraction of the features it has today. Even ANSI C was still brand new.
    – Jan Hudec
    Aug 8 '13 at 7:11
  • @PieterGeerkens hence the first edition of C++ by Stoustrup, "C with Classes". His first compiler was written in C with the aforementioned macros. Aug 8 '13 at 18:10
  • @JanHudec I still have a book with (roughly) the title "Professional programming in C++", printed in 1992. It even came with a 3.5" floppy disk with all the source code from the book, in UNIX (Unix compress .Z compressed tarball) and DOS formats. I wonder if that floppy would still be readable? The language was simpler back then, and much of that book focuses on how to use classes and inheritance in your own code.
    – user
    Aug 8 '13 at 21:27

The idea of "function pointers in a struct along with fields" is a version of "modular programming", which is what came between "structured programming" and "object oriented programming"

C and Pascal were not "modular programming" languages. Pascal went to Modula, Delphi and ADA, which were, amongst other things, modular programming languges, but C never went there. Instead, C eventually jumped directly to the next step, C++, an Object Oriented language.

Many people immediately noticed that C++ was "a better C". There are a couple of reasons for that, but I think it was mostly because of the support for modular programming.

In C, the basic module/object is the File/Compilation Unit. In C++ there is language support for breaking your code up into modules (objects). You might call this OO, but if you aren't using inheritance or polymorphism, your OO is only the encapsulation everyone else had already, and nobody thought that Modula was an Object-Oriented Programming language.

You can of course do modular programming in C and ISO Pascal, but you can do OO and Generic programming in interpreted BASIC. What we mean when we say that C++ is an OO language is that the language does it for you, not that you can do it to the language.

For me, the simple point is that when doing OO in Pascal, I had to write my own vtables. It's the same in C: if you want a virtual method table, you have to maintain it yourself. So was C designed to facilitate OOP? If it was, where at the vtables? Cadit quaestio.

BTW, C++ is a Generic programming language as well as Modular and OO. Generic Programming has an even longer pedigree, but...

  • 5
    "Many people immediately noticed that C++ was "a better C"." I am quite sure that there are plenty of C programmers who would take issue with the word "noticed" in that statement... maybe "believed"? Aug 8 '13 at 18:07

C (and also C++) started out at AT&T. Their 5ESS telephone switch was an early user of C, and it definitely uses C and OOP. The two sides of a call (Originating Party and Terminating Party objects) could exchange messages, and there was a large overlap in the set of messages used. However, it did not use the techniques you describe here. It was all hand-coded.

So, OOP dates back to the early days of C, the two techniques were combined in a single product, and K&R must have known about this. Yet they did not facilitate OOP in C as it was used at the time, in their company.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.