54

I was talking with a coworker about C and C++, and he claimed that C is object-oriented, but I claimed that it was not. I know that you can do object-oriented-like things in C, but C++ is a true object-oriented language.

What are your thoughts?

Also, it triggered discussion on who decides what it means to be object-oriented, and that it's tough to say what object-oriented really officially means. What are you thoughts on this?

18
  • 53
    Get everyone to agree on a definition of Object Oriented and then I will be able to answer the question. Commented Jul 13, 2010 at 22:08
  • 5
    Object-oriented programming (OOP) is a programming paradigm that uses "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction, encapsulation, modularity, polymorphism, and inheritance. It was not commonly used in mainstream software application development until the early 1990s.[citation needed] Many modern programming languages now support OOP. Commented Jul 13, 2010 at 22:09
  • 8
    That is just one definition. Gotta love the [citation needed] btw ;) Commented Jul 13, 2010 at 22:09
  • 5
    @Yacoby, @Brian: Most people agree on a handful of concepts like inheritance, encapsulation and polymorphism (did I forget one?) The discussion on C is dead simple because C features essentially none of those things. Commented Jul 13, 2010 at 22:10
  • 9
    C has no built-in string type, only arrays of char. Would you claim that C lacked strings? Commented Jul 13, 2010 at 22:15

14 Answers 14

79

If by "is C object-oriented?" you mean "is C designed with facilities specifically to support object-oriented programming?" then, no, C is clearly not object-oriented.

Sign up to request clarification or add additional context in comments.

4 Comments

+1 The only way to say a language is "insert paradigm" is to assert if it facilitates or enforces the paradigm with language constructs.
Is C object-based?
Please examine SunView, XView, Motif, Xaw, and Xt before boldly claiming that "C is clearly not object oriented" I'm not claiming that C is object-oriented, it's just that people have jumped through hoops to make C object-oriented.
COOP Can add Object Oriented syntax to C with minimal overhead.
21

You can program in an object-oriented style in more or less any language. (I think runtime polymorphism—i.e., virtual methods—requires a language that supports function pointers.)

Here are a couple of examples:

3 Comments

I remember a colleague once reading about OO Perl. I skimmed the book, and my soul shuddered
Re my function pointers comment: I challenge anyone to program OO in BASIC (the original, not this Visual stuff)
The www.emilmont.net link is broken (including as HTTPS): "Hmm. We’re having trouble finding that site. We can’t connect to the server at www.emilmont.net."
12

C isn't object-oriented. That was the entire purpose behind the ++.

As far as a definition of what it takes to be object-oriented: check Wikipedia.

Personally, if it supports inheritance, encapsulation, and polymorphism then you’re good to go. Another key here is having nice keywords, like class and object tend to help...

Examples of real object-oriented languages (not conclusive) are: Smalltalk, Java, C#, Python, Ruby, and C++.

Also, it's possible to have extensions to provide OO features, like PHP, Perl, Visual Basic (not VB.NET), etc.

7 Comments

Please explain ... I don't want just plain no for an answer.
If those are "real" OO languages, are there fake ones?
@Roger Pate: the "fake" ones are those that have an OO veneer like vb.net and php. They aren't built up from an OO perspective, but do provide at least some facility for OO development.
Can you clarify what you mean about the OO veneer for VB.NET?
@roger fake OO Language = Mod Perl
|
10

Real programmers can write object-oriented code in any language.

But no, C is not an 'object-oriented' language. It doesn't have any concept of classes, objects, polymorphism, or inheritance.

2 Comments

I argee - its possible to write OO code in any language. I recently refactored a legacy C program to divide the modules into namespaces, and put all relevant code and variables together. Its amazing how it increased the maintainability of the program - it had to be seen to be believed (admittedly it required a C++ compiler to support namespaces, but I believe namespaces should be part of C in any case).
@Contango: Are you sure they're not? I don't use C much, but whenever I do I'm always surprised at how many features have been added in the past 30 years that people treat as if they don't exist. IE, resizable arrays (C09?) and const variables (C89).
7

The answer can be yes or no, depending on:

  • if you ask "is C an object-oriented language?", the answer is "no", because it does not have object oriented constructors, keywords, semantic, etc.

  • if you intend "can I realize OOP in C?", the answer is yes, because OOP is not only a requirement of a language, but also a way of "thinking", an approach to programming, before to touch some language or another. However, the implementation of OOP in C (or any other language not natively designed to be OOP) will be surely "forced" and much harder to manage than any other OOP language, so also some limitations shall be expected.

Comments

4

Unless your friend was talking about Objective-C (an OO superset of C) then no, C isn't an OO language. You can implement OO concepts using C (that's what the old Cfront C++ compiler did; it translated C++ into C), but that doesn't make C an OO language as it doesn't specifically offer support for standard OO techniques, like polymorphism or encapsulation.

Yes, you can write software OO style in C, especially with liberal (ab-)use of macros, but as someone who has seen the results of some of those attempts, I'd strongly suggest to use a better suited language.

Comments

4

C is not an O-O language under any definition of "O-O" and "language".

It is quite easy to use C as the implementation language for a component that gives an O-O API to its clients. The X Window system is essentially a single-inheritance O-O system when viewed from its API, but a whole mess of C when viewing its implementation.

1 Comment

This view is adequately expressed by the existing answers.
3

The confusion may be that C can be used to implement object-oriented concepts, like polymorphism, encapsulation, etc., which may lead your friend to believe that C is object-oriented.

The problem is that to be considered an object-oriented programming language, these features would need to be built into the language. Which they are not.

Comments

3
  1. C is not object-oriented (OO) in strict sense since it doesn't have a built-in syntax supported object oriented capability, like class, inheritance and so on.

But if you know the trick, you can easily add object-oriented capability to it simply using struct, function pointer, and self-pointer.

DirectFB is such a C library written in an object-oriented way. The bad thing it is more error-prone since it is not governed by syntax and compile-time type checking. It is based on coding conventions instead.

E.g.,

  IDirectFB /* A typedef of a struct */ *dfb = NULL;

  IDirectFBSurface /* Another typedef of a struct */ *primary = NULL;

  DirectFBCreate (&dfb); /* A factory method to create a struct
                            (e.g., dfb) with pointers to function
                            and data. This struct is like an
                            object/instance of a class in a
                            language with build-in syntax
                            support for object-oriented
                            capability */

  dfb->SetCooperativeLevel /* Function pointer */
          (dfb /* Self pointer to the object dfb */,
           DFSCL_FULLSCREEN);
  dsc.flags = DSDESC_CAPS;
  dsc.caps  = DSCAPS_PRIMARY | DSCAPS_FLIPPING;

  dfb->CreateSurface /* A function pointer, but also a factory method
                        to create another object/instance */
          (dfb /* Self pointer to the object dfb */,
           &dsc,
           &primary /* Another struct work as object of
                       another class created*/ );

  primary->GetSize /* A function pointer */
              (primary /* A self pointer to the object primary */,
               &screen_width,
               &screen_height);
  1. C++ is object-oriented since it has built-in support for object oriented capability like class and inheritance. But there is argument that it is not a full or pure object oriented language since it does allow C syntax (structural programming syntax) in it. I also remember that C++ lack a few object-oriented capabilities, but I do not remember each one exactly.

Comments

3

C is not an object-oriented language.

C is a general-purpose, imperative language, supporting structured programming.

Because C isn't object-oriented, therefore C++ came into existence in order to have OOP features and OOP is a programming language model organized around objects.

A language, in order to have OOP features, needs to implement certain principles of OOPs. A few of them are inheritance, polymorphism, abstraction, and encapsulation.

Comments

2

C is an object-based language. It does not support many features of object-oriented (OO) languages, such as inheritance, polymorphism, etc.

Comments

0

C is not object-oriented (OO).

C does not orient to objects.

C++ does.

Comments

0

Though C itself was built as a procedural language (it "thinks" in terms of a procedure: You first execute function A and then you pass the output to function B, etc. It supports an "out of the box" only functional program flow), It's possible to implement OOP over C (in OOP, the story is driven by objects and their responsibilities rather than functions and their calling order).

In fact, some early C++ implementations were translating the code to some C code and then building it.

However, sometimes you must use C (for embedded devices / GPU languages that don't support C++, etc). And you want OOP. I'd suggest you to check out COOP. My C OOP lightweight, yet powerful, framework for C object-oriented programming.

Comments

0

C is not object-oriented. C++ is not object-oriented. Let me explain:

Object-oriented is an extension to Simula's old-fashioned event-driven subset. Real object-oriented are functional and reflective, because object-oriented is really a group of paradigms (event-driven, functional, and reflective).

Only four languages are really object-oriented and these are Lisp, Smalltalk, Ruby and OCaml. Perl lags behind, because its not functional. Scala is not reflective, so it also lags behind. C++ is only event-driven with some Simula-like facilities, but it’s completely structured programming language, and it’s not declarative or even matches the real world.

Object-oriented matches the real world with functional (mathematics), event-driven (conversations) and reflectiveness (evolution).

C++ only has conversations. C++ is not declarative, like mathematics, or doesn’t evolve like life. C++ only converses like people. C++ is like an idiot that doesn’t know how mathematics work or how life evolves.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.