52

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?

Brian T Hannan
  • 3,925
  • 18
  • 56
  • 96
  • 48
    Get everyone to agree on a definition of Object Oriented and then I will be able to answer the question. – Yacoby Jul 13 '10 at 22:08
  • 4
    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. – Hamish Grubijan Jul 13 '10 at 22:09
  • 8
    That is just one definition. Gotta love the `[citation needed]` btw ;) – Georg Fritzsche Jul 13 '10 at 22:09
  • @Hamish : right, but who ultimate decides the definition of OOP? – Brian T Hannan Jul 13 '10 at 22:10
  • 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. – Carl Smotricz Jul 13 '10 at 22:10
  • 1
    Holy Crap, Simula is too cool for 1960s!!! Just look at the code samples. http://en.wikipedia.org/wiki/Simula Unfortunately neither Dahl, nor Nygaard could make it to the ACM Turing Award Lecture, scheduled to be delivered at the OOPSLA 2002 conference in Seattle, as they both died within two months of each other in June and August, respectively. – Hamish Grubijan Jul 13 '10 at 22:11
  • Are you sure he's not talking about Objective-C – Joshua Partogi Jul 13 '10 at 22:12
  • 1
    I'm not sure why anyone is down-voting this. This is a legitimate question because there are standards out there, but no official one that people should follow like the law. There are also many Software standards that don't have to be followed or are refused to be followed by many great programmers. – Brian T Hannan Jul 13 '10 at 22:13
  • 9
    C has no built-in string type, only arrays of `char`. Would you claim that C lacked strings? – Tim Robinson Jul 13 '10 at 22:15
  • See, Tim Robinson is on the right train of thought I'm looking for. – Brian T Hannan Jul 13 '10 at 22:16
  • @Brian I seem to be in a minority though – Tim Robinson Jul 13 '10 at 22:19
  • 1
    @Tim: C has string literals, but no strings. :-P – Laurence Gonsalves Jul 13 '10 at 22:26
  • @Brian: Regarding standards: Definitions like OOP are decided by both the creators of the languages we use and the marketplace. The OOP definition is pretty much a done deal, through an organic process the industry put the feature list to be called OOP to bed years ago. If you want a definitive answer, call Microsoft, Watcom, and any other C vendor you can think of and ask them if *they* consider C to be OOP. The answer is going to be "no" – NotMe Jul 13 '10 at 22:34
  • 1
    @Laurence Gonsalves: Oh? What, then, is the ubiquitous _C string_ if it is not a string? – James McNellis Jul 13 '10 at 22:40
  • Related, but not duplicate: [What Makes a Language Object-Oriented?](http://stackoverflow.com/questions/29099/what-makes-a-language-object-oriented), [Can you write object oriented code in C?](http://stackoverflow.com/questions/351733/can-you-write-object-oriented-code-in-c), [Object-Orientation in C](http://stackoverflow.com/questions/415452/object-orientation-in-c) – James McNellis Jul 13 '10 at 22:46
  • 1
    @James McNellis: See http://stackoverflow.com/questions/2283900/what-is-the-difference-between-a-char-array-and-a-string In particular look at Draemon's answer – NotMe Jul 13 '10 at 22:55
  • @Tim: Would you claim C is a string-oriented language then? ;) – Georg Fritzsche Jul 15 '10 at 00:22
  • Yes, as much as assembly (which C++ compiles down to) is an OOP language. And considering it was once a preprocessor to C… [Cfront](https://en.wikipedia.org/wiki/Cfront) — you can thank it for the name mangling — you do have to wonder if C is just an incomplete OOP language ^^; – Lloyd Sargent Jun 02 '21 at 20:09

16 Answers16

76

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.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • 31
    +1 The only way to say a language is "insert paradigm" is to assert if it facilitates or enforces the paradigm with language constructs. – pmr Jul 13 '10 at 22:17
  • Is C object-based? – Luca Sans S Feb 18 '21 at 15:58
  • 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. – John Carlson Aug 07 '21 at 01:54
  • [COOP](https://github.com/ShmuelFine/COOP) Can add Object Oriented syntax to C with minimal overhead. – Shmuel Fine Dec 21 '22 at 20:17
20

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:

Tim Robinson
  • 53,480
  • 10
  • 121
  • 138
  • 6
    I remember a colleague once reading about OO Perl. I skimmed the book, and my soul shuddered – johnc Jul 13 '10 at 22:19
  • 2
    Re my function pointers comment: I challenge anyone to program OO in BASIC (the original, not this Visual stuff) – Tim Robinson Jul 13 '10 at 22:46
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 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), ...

NotMe
  • 87,343
  • 27
  • 171
  • 245
  • 1
    Please explain ... I don't want just plain no for an answer. – Brian T Hannan Jul 13 '10 at 22:09
  • 2
    If those are ["real"](http://stackoverflow.com/questions/3222316/what-is-a-real-programming-language/3226182#3226182) OO languages, are there fake ones? –  Jul 13 '10 at 22:43
  • 4
    @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. – NotMe Jul 13 '10 at 22:50
  • Can you clarify what you mean about the OO veneer for VB.NET? – Jordan Jul 13 '10 at 23:07
  • @roger fake OO Language = Mod Perl – Michael Mullany Jul 13 '10 at 23:51
  • How is VB.NET and PHP not oo? – Will03uk Oct 18 '11 at 21:36
  • @Will03uk: For PHP look at http://michaelkimsal.com/blog/php-is-not-object-oriented/ Basically, OO features are bolted on and completely optional, hence, it's really 'object oriented' For VB.Net.. well, I mistyped. Should have been simply VB. – NotMe Oct 18 '11 at 21:44
10

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.

Roddy
  • 66,617
  • 42
  • 165
  • 277
  • 2
    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 Jul 13 '10 at 23:25
  • @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). – ArtOfWarfare Apr 11 '15 at 17:43
7

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.

bzimage
  • 71
  • 1
  • 5
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 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.

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.

Graphics Noob
  • 9,790
  • 11
  • 46
  • 44
3

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.

Timo Geusch
  • 24,095
  • 5
  • 52
  • 70
3
  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.

ttchong
  • 307
  • 2
  • 9
3

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.

2

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

1

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"?

newbie007
  • 145
  • 1
  • 2
0

C is not Object Oriented.

C does not orient to objects.

C++ does.

Van Tr
  • 5,889
  • 2
  • 20
  • 44
0

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.

Shmuel Fine
  • 351
  • 1
  • 5
0

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.

Gallu Xddd
  • 21
  • 1
-1

No, it is not, your friend is wrong.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • Please explain ... I don't want just plain no for an answer. – Brian T Hannan Jul 13 '10 at 22:08
  • Well, the language does not directly object oriented programming, so it seems pretty obvious to me. – Ed S. Jul 13 '10 at 22:16
  • 3
    "Is the C programming language object-oriented?" "No" "Why?" "Because it is not" – devoured elysium Jul 14 '10 at 00:39
  • Well, it's not. Even a cursory understanding of what object oriented programming is about would tell you that, – Ed S. Jul 14 '10 at 05:27
  • C is no OO since it lacks syntactic sugar and runtime support for OOP? (maybe can be said this way); if you want, you can implement a runtime and use "normal" syntax (or macros or helper functions)... so maybe it can be said it is OO capable, ... but currently no OO features are builtin-embedded. – ShinTakezou Jul 14 '10 at 06:28
  • Yup, and if you want, you can pound in nails with a screw driver, but that would just be stupid. – Ed S. Jul 14 '10 at 17:13