C vs. C++: 10 key differences
C++ was conceived as ‘C with classes’. Indeed, C++ was released in 1985 as an extension of the C programming language. However, the differences between these two fundamental programming languages are revealed better when compare them in terms of the most useful practical aspects along with conceptual points of view.
Let’s start with general approaches and then move to the practical side.
Paradigm
- C is procedural programming language. It focuses on procedures and functions.
- C++ is multi-paradigm programming language. It supports procedural, object-oriented, and generic programming paradigms, so is quite universal and aimed at improving the way of programming in general.
Object-Oriented Programming (OOP)
- C, honestly, lacks built-in support for OOP features like classes, objects, inheritance, polymorphism.
- C++ supports OOP concepts such as classes, objects, inheritance, polymorphism, encapsulation. All that is needed for building complex, fast and convenient to maintain applications.
Encapsulation
- C doesn’t have built-in support for encapsulation. Data and functions are typically defined globally or within other functions (e.g., nested functions).
- For C++ encapsulation is a core principle. Classes allow data hiding by making data members private and providing public methods for accessing or modifying them. One more point here: in C members of struct are public by default. In C++ for classes ‘private’ keyword is used by default, but for structures — ‘ public’.
Inheritance
- C doesn’t support inheritance. At all.
- C++ supports single, multiple, hierarchical, and multilevel inheritance. For classes it is private by default, for structures — public. Also, C++ has protected inheritance: public members and functions become protected, protected remain protected, and private are private.
Polymorphism
- C doesn’t support polymorphism. So sad.
- C++ supports compile-time (function overloading) and runtime (virtual functions) polymorphism.
Function Overloading
- C doesn’t support function overloading. Again, no.
- C++ allows defining multiple functions with the same name but different parameter lists. Note that the return type is disregarded here.
Pointers and References
- C supports pointers only.
- C++ supports both pointers and references. References provide a safer alternative to pointers and are often used in C++ code.
Memory Management
- C supports manual memory management using functions like
malloc()
andfree()
for allocating and releasing. - C++ supports both manual and automatic memory management using RAII (Resource Acquisition Is Initialization) and smart pointers (e.g.,
std::unique_ptr
,std::shared_ptr
).
Standard Libraries
- For C standard library includes functions for basic input/output, string manipulation, memory management, etc. Not too much, but we have what we have.
- C++ extends the C standard library and includes additional libraries for various purposes, such as containers (e.g., vectors, lists, maps), algorithms, input/output streams, concurrency (e.g., threads), etc. That’s nice, isn’t it?
Error Handling
- C uses error codes and return values for error handling. Cool, but honestly, we need more.
- C++ supports exceptions for error handling, which allow for cleaner and more structured error handling compared to error codes.
Conclusion
So, C is procedural programming language with ‘beginnings’ of an object-oriented approach realised with structures for data handling. It doesn’t support inheritance, encapsulation and polymorphism, as far as function overloading, and works with pointers which are not too safe. In C we can only work with manual memory management and handle errors via return values and error codes. Standart library for this language isn’t reach enough.
In contrast, C++ is object-oriented language with both manual and automatical memory management. It supports inheritance, encapsulation (public, protected and private specifiers), polymorphism (compile-time via function overloading and runtime via virtual functions). Moreover, in C++ we can use exceptions for error handling and links as safer alternative to pointers. STL (standart template library) for C++ includes the most popular data structures, methods and algorithms for using on daily basis. One more point: C++ supports templates and rapidly developed in the metaprogramming paradigm.
To sum up, 50+ years old C is still alive and is widely used in embedded applications. 40 years old C++ is more promising — not only for embedded systems, but also for AAA games, graphical engines and every area where time and productivity matters.