-3

In a recent interview I was asked a question that what is the difference between Class and Structures which led me to think that if the difference is just about access specifier and we can use struct keyword in C then why isn't C object oriented. Can anyone solve my doubt? This doubt might be naive but curiosity has it's own way.

  • 1
    cross site dupe: https://softwareengineering.stackexchange.com/questions/113533/why-is-c-not-considered-an-object-oriented-language – NathanOliver May 26 '21 at 20:34
  • related/dupe: https://stackoverflow.com/questions/3241932/is-the-c-programming-language-object-oriented – NathanOliver May 26 '21 at 20:35
  • 2
    What language features are in your opinion required in order to call a language object oriented? Encapsulation? Inheritance? Polymorfism? – Support Ukraine May 26 '21 at 20:36

4 Answers4

4

A problem with this question is that in order to give an answer, we first need to define "what is an object oriented language".

An OO language like C++ has many features mapped into the language but that doesn't mean that any language need to have all thoses feature to be considered object-oriented.

What is required?

  • Encapsulation?
  • Inheritance?
  • Polymorfism?
  • ... and so on ...

To me one of the most important features is encapsulation. With encapsulation you tie together the data that describes the object and the operations you can do with the object.

For instance a car object. It will have data members to describe its color, type of gear, number of gears, number of wheels, current speed and so on. Then it will have (member) functions to operate the car. You can accelerate, break, turn left and so on. The operations and the data are encapsulated, i.e. when an operation is carried out on an object by calling a function, all data describing the object is available to the function.

This encapsulation is a key feature of object oriented programming.

Encapsulation can't be handle by C.

We can make a struct with data members. We can even add (member) functions (aka function pointers) for the operations. So using proper initialization of a C struct object, we can write OO-style code like

mycar.getSpeed();

but...

C has no automatic relation between the members. We can't call a member function and automatically have access to the data of that object. C has no concept of encapsulation.

In C when calling a member function that needs access to data members of the same object, we need to pass (the address of) the object itself in order to get access to the data. Like:

mycar.getSpeed(&mycar);
\------------/ \^^^^^/
   oo style      but explicit passing pointer to
    call         the object violates the encapsulation principle

In a object oriented language this is often called the this pointer, and it is available implicit - you don't need to pass it. But in C you will have to pass it explicit. So the benefit of using function pointer members to get OO-style operations is (typically) small - we can just as well do:

getSpeed(&mycar);

The lack of encapsulation is one reason that C isn't object oriented.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
3

In C++, the main difference between a struct and a class is that members of a struct are public by default, while members of a class are private by default.

A struct in C is different because it cannot contain member functions nor does it support inheritance. It is only a means of aggregating variables of differing types. While it can contain function pointers, they are not inherently tied to a given object like member functions in C++.

While the C language defines an "object" as "region of data storage in the execution environment, the contents of which can represent values" (C11 3.15), a struct is not an object in terms of being object-oriented, i.e. an encapsulated set of data and the functions that operate on them.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • Private/public defaults are largely cosmetic, not a main issue. Member functions and inheritance are a main issue. (And they are only defaults in C++; in C, there is no private option.) – Eric Postpischil May 26 '21 at 21:12
1

In C++ class and struct are keywords that can be used to declare a class. The fact that C is missing the keyword class does not make it less object oriented. What does it make "less object oriented" is that C structures cannot have member methods, in particular they do not have constructors nor destructors.

However, there is no clear cut between object oriented and non-object oriented languages. Object orientation is a paradigm, and languages can natively support it more or less. For example, is Python not object oriented because it has no "real" encapsulation? Certainly not. Moreover, consider that C++ started as C with classes. Many object oriented features that are built-in in C++ can be emulated in C.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • So the keyword `class` is required for a language to be object oriented? BTW: C structs can have function pointer members called just like c++ member functions. – Support Ukraine May 26 '21 at 20:42
  • @4386427 - Bar the whole passing of an implicit `this` parameter, and other minutiae. – StoryTeller - Unslander Monica May 26 '21 at 20:57
  • Seriously? No clear distinction between object oriented and non-object oriented languages? – kadina May 26 '21 at 21:05
  • @StoryTeller-UnslanderMonica Yes, the `this` pointer.... the missing encapsulation... I've posted just that as an example of why C isn't object oriented. Read it if you are interrested - feedback welcome. – Support Ukraine May 26 '21 at 21:12
  • @kadina yes, seriously. I found [this](https://en.wikipedia.org/wiki/Object-oriented_programming) and they list languages ranging from "everything is an object" to "supports some OO features". No clear cut. And I doubt such a clear cut definition would be of any practical use, because it would be either too broad or too narrow – 463035818_is_not_an_ai May 26 '21 at 22:52
1

why isn't C object oriented

Short answer - C wasn't designed with object-oriented programming in mind. C++ was.

The chief difference between a struct in C and a class or struct in C++ is that you can't (easily) associate code with a struct in C. You can't execute a method on a struct instance in C, such as

list.sort(); 

or

sequence.average();

Instead, you have to pass those struct instances as arguments to functions, like

sort( &list );        // have to pass a pointer since we're going to modify list
average( sequence );

C just doesn't provide the kinds of tools that make object-oriented programming easy - no encapsulation, no inheritance, no polymorphism (well, there's _Generic, but it's not quite the same).

You can do object-oriented programming in C, but it is a lot of work for arguably little benefit.

John Bode
  • 119,563
  • 19
  • 122
  • 198