Is the C programming language object-oriented?

COopStandards

C Problem Overview


I was talking with a co-worker 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?

C Solutions


Solution 1 - C

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.

Solution 2 - C

You can program in an object-orientated 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:

Solution 3 - C

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 your 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, C++..

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

Solution 4 - C

> Real programmers can write object-oriented code in ANY language.

But no, C is not an 'object-oriented' language. It has no concept of classes, objects, polymorphism, inheritance.

Solution 5 - C

Answer can be yes or no, depending on:

  • if you ask "is C an object oriented language?", the answer is "no" because it do 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 hard to manage then any other OOP language, so also some limitation shall be expected.

Solution 6 - C

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 Windows system is essentially a single-inheritance O-O system when viewed from its API, but a whole mess of C when viewing its implementation.

Solution 7 - C

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.

Solution 8 - C

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.

Solution 9 - C

  1. C is not object oriented 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, & 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 type checking. It is based on coding convention instead.

e.g.

  IDirectFB/*a typedef of a struct*/ *dfb = NULL;
  IDirectFBSurface/*another typedef of a struct*/ *primary = NULL;
  DirectFBCreate (&dfb); /*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/*function pointer, 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/*function pointer*/ 
              (primary/*self pointer to the object primary*/, 
               &screen_width, 
               &screen_height);

2 . 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 not remember each one exactly.

Solution 10 - C

C is not 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 OOPs feature and OOP is a programming language model organized around objects. A language in order to have OOPs feature needs to implement certain principles of OOPs.Few of them are Inheritance, Polymorphism, Abstraction , Encapsulation.

Solution 11 - C

> Real programmers can write object-oriented code in ANY language.

I have seen Object Oriented Cobol. Cobol that calls Cobol. Do you want to call these programmers "Real"?

Solution 12 - C

C is a object based language, it does not support many features of object oriented languages such as inheritance, polymorphism etc.

Solution 13 - C

C is not Object Oriented.

C does not orient to objects.

C++ does.

Solution 14 - C

Though C itself was bulit as procedural language (it "thinks" in terms of procedure: You first execute function A then you pass the output to function B etc. it supports "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.

Solution 15 - C

C is not object oriented. C++ is not object oriented. Let me explain: Object Oriented is an extension to Simula's old fashion event driven subset. Real Object Oriented are functional and reflective because Object Oriented is really a group of paradigms (event driven, functional, reflective). Only four language are really object oriented and these are Lisp,Smalltalk,Ruby and Ocaml. Perl lags between because its not functional. Scala is not reflective so also lags behind. C++ is only event driven with some Simula like facilities but its completely structured programming language and its not declarative or even matchs real world. Object Oriented matchs real world with Functional (Maths), Event Driven (Conversations) and Reflectiveness (Evolution). C++ only has conversations. C++ is not declarative like maths or doesnt evolve like life. C++ only converses like people. C++ is like an idiot that doesnt know how maths work or how life evolves.

Solution 16 - C

No, it is not, your friend is wrong.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrian T HannanView Question on Stackoverflow
Solution 1 - CJames McNellisView Answer on Stackoverflow
Solution 2 - CTim RobinsonView Answer on Stackoverflow
Solution 3 - CNotMeView Answer on Stackoverflow
Solution 4 - CRoddyView Answer on Stackoverflow
Solution 5 - CbzimageView Answer on Stackoverflow
Solution 6 - CBill TorcasoView Answer on Stackoverflow
Solution 7 - CGraphics NoobView Answer on Stackoverflow
Solution 8 - CTimo GeuschView Answer on Stackoverflow
Solution 9 - CttchongView Answer on Stackoverflow
Solution 10 - CVarunnuevothoughtsView Answer on Stackoverflow
Solution 11 - Cnewbie007View Answer on Stackoverflow
Solution 12 - CRituraj SahaView Answer on Stackoverflow
Solution 13 - CTrevorView Answer on Stackoverflow
Solution 14 - CShmuel FineView Answer on Stackoverflow
Solution 15 - CGallu XdddView Answer on Stackoverflow
Solution 16 - CEd S.View Answer on Stackoverflow