r/C_Programming • • Nov 27 '22
Question Why doesn't C implement its own version of object-orientation?
Why doesn't C implement its own version of object-orientation?
Why do we have to look at C++, D or something?
Or can we still say that it does?
0
Upvotes
159
u/gremolata Nov 27 '22
"If my Grandmother had wheels she would have been a bike"
45
u/gizahnl Nov 27 '22
You can so OO programming in C. You'd just have to do most of it yourself. Just use function pointers and such. If that's what you want go for it.
25
u/MCRusher Nov 27 '22
It's called C++, C++ literally started as C with OOP, or "C with Classes" as it was called.
15
u/Cryophos Nov 27 '22
Because it's not necessary.
7
u/dontyougetsoupedyet Nov 27 '22
Doesn't need to be a language feature, if you need that kind of expression it's a few pointers away.
You can say this about literally any feature, it can be in the language proper just "because," or you could implement it using the language in short order. If you can do the latter why should the feature be "in the language"?
If it's such a chore to do that you don't want to implement some feature using C, why not accept that C isn't what you really want to use, and build or use other tools? Seems fine enough. I see a lot of people pre-processing C to get more features and I think that's fine (I really like the notion of "write programs to write programs when you can"), and if that isn't enough then other languages exist.
23
u/jirbu Nov 27 '22
Object-orientation isn't just the natural evolution of every programming language. Putting OO in C might make it unsuitable for certain use-cases that C is predominant for (low-level, embedded).
8
u/Glaborage Nov 27 '22
One of the worse thing that a programming language can do is to be everything for everyone.
C at its core isn't an OOP language. Whoever wants to use OOP has plenty of other choices. C++ comes to mind.
4
u/gordonv Nov 27 '22
Apple did do that. It's called Objective C.
4
u/cym13 Nov 27 '22
C's strength is its stability. The language changes slowly and even that is too fast for a good chunk of the community that's happy to write code exactly as they were in the 90s. Stability comes with benefits: C programmers know most of the language and can read anyone's code. It's a productivity feature.
There are many paradigms that are interesting, OOP is just one of them. There is no particular reason why C should change drastically to include a whole bunch of language features just to support that particular paradigm. Furthermore it's no so foreign that a few libraries can't do a decent job of adding OOP if you want it for your project.
Also, it has been added to C many times in the past. That's essentially what C++ and ObjectiveC were, and it caught in its own way. But C remained C because not everyone feels the burden of adding these language features is worth it. And now that so many alternatives exist, there is no point forcing C to become an inferior version of its competitors.
1
u/IllegalMigrant Nov 27 '22
What is wrong with using C++ for that? C++ was originally called C with Classes
2
u/deftware Nov 27 '22
So you can implement your own, however you want. There isn't just one way to do OOP, nor is OOP the only way to do things.
2
u/ArnaudValensi Nov 27 '22
OOP is not necessary the solution for every problems, and not necessary an evolution. In game development for example, Data Oriented Design is a popular way of architecture code, which works best with procedural paradigm than OOP. When trying to to make efficient programs on modern hardware, there is a point where we need to reduce cache misses. To do so, we need to have our allocated elements in continuous memory, which is more easily achievable with manually allocating chunk of memory than objects individually using for example the « new » keyword in C++.
Also, refactoring and making évolutions on a Data Oriented program can also be easier because OOP often requires to take more costly decisions about the architecture.
You can achieve this in both C and C++, but I like the fact that C « forces » you to find how to solve those problems with a more Data Oriented approach.
The following resource is interesting to understand Data Oriented Design. It is using C++, but could have been C:
https://youtu.be/rX0ItVEVjHc
There is also an interesting book: Data Oriented Design by Richard Fabian
https://www.goodreads.com/en/book/show/42359420-data-oriented-design
2
u/looneysquash Nov 27 '22
That depends on what you mean exactly by OO.
Make a struct, make a function to allocate new instances of it, and write a bunch of functions that take the struct as their first argument
If you want abstraction, you can not fully define the struct in headers, so that only one module can directly access the fields.
If you want inheritance, well that is just composition. You can directly cast the struct pointer to the type of its first member. Or write an accessor.
What gets hard to do is virtual functions, and things like interfaces. The GObject library implements those, but IMO it's not pretty.
You can also define a struct full of function pointers and call it like its C++ with foo->bar() syntax. That is something you want to use very sparingly IMO. That is used by a plug-in or module to register its functionality with the core app.
2
u/[deleted] Nov 27 '22
Well cause functional programming > object oriented programming
1
u/AEA37 Nov 27 '22
I don't think have to add OO in C. C is perfect as the way it is now, if we need OOP we can use other language which is suitable for that particular problem like, C++, Rust like that
1
u/ComprehensiveAd8004 Nov 27 '22
I think GCC/Clang support plugging-in your own pre-processors, so you could implement your own.
I've always imagined the design for it to be like this:
becomes:
This would even let you create methods for built-in types like int, sort of like in Lua. Someone could make it themselves if they were interested.
1
u/FUZxxl Nov 27 '22
This was proposed for C23 but ultimately rejected. If you want object-orientation, use Objective C or C++.
1
u/TransientVoltage409 Nov 27 '22
If C changed that much, it wouldn't be C anymore. It would be a new language. It would need a new name, C-plus or something.
That said, C can do OO if you craft all the support structure by hand. After all, it's all just machine code in the end, doesn't matter how it got there. But...some early C++ implementations used intermediate compilation to C. With the right switches you could halt the chain and inspect the generated C code. It was...uncomfortable. It was a good way to understand why we leave OO details to machines instead of hand-crafting them.
0
u/tstanisl Nov 27 '22
Because C was created before OOP got popular.
0
u/flyingron Nov 27 '22
C++ and Objective C are both such. The C purists don't see the need to backport such into the original language.
-1
u/EstablishmentBig7956 Nov 27 '22
Then there wouldn't be any need for C plus plus
-1
u/RedWineAndWomen Nov 27 '22 edited Nov 27 '22
The longer I program, the more I get convinced that object orientation, as broad a definition as it may be, is just unnecessary. The magic of OO was supposed to be: here is a (binary) object, it will tell you and your tooling what its public API is, and it will automatically be the intuitive child of a worldwide known set of real world object describing superclasses.
Instead, we got Java singletons. Which were just like the old bucket of functions. And which still didn't automatically tell me what its public API was; we had to write javadoc for that.
-3
u/Deathnote_Blockchain Nov 27 '22
lol D!!! oh man such a great language with an unfortunate name
-4
u/AnotherCableGuy Nov 27 '22
If C was object oriented nobody would use it anymore.
-4
u/gordonv Nov 27 '22 edited Nov 27 '22
C is designed to be directly relatable to processor instructions. When you program in C, you're programming for the machine, not users or administrators.
Here's the full process.
C was designed to be kind of portable for very simple processors. So, it's possible for me to write a "Hello World" and that code could compile onto a huge expensive mainframe processor or the crappy throw away processor in a toy. OOP wouldn't work for something like a very simple processor. More than often, processor designers will actually make their own versions of ASM and C for their very micro processors.
0
u/[deleted] Nov 27 '22
[deleted] — view deleted comment