C vs C++

Updated: 2021-11-19

C++ is NOT a superset of C

C++ began as a fork of an early, pre-standardized C.

Incompatibilities can be found in the wiki page: https://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B

Release Cycles

  • C99, C11, C17, C23
  • C++98, C++03, C++11, C++14, C++17, C++20, C++23

Procedural vs Object-oriented

  • C++ is object-oriented.
  • C is procedural.

Security

  • C++ supports encapsulation.
  • In C, data can be accessible by other entities.

Headers and namespace

e.g. C uses <stdio.h>, C++ uses <iostream.h>

There's no namespace keyword and concept in C. When importing headers to the program, the traditional C-style .h headers will go to the global namespace. C++ style headers will go into the standard std namespace.

Your C++ program should avoid using C-style headers, use the C++ version instead (e.g. use cstdio instead of stdio.h):

#include <cstdio>

int main() {
  std::printf("Hello World\n");
}

Another difference is that the C headers don't have overloaded versions of some functions because C does not allow function overloading. E.g. <math.h> only has one version that takes doubles, while <cmath> have different versions for ints, floats and doubles.

Overloading / Overriding

  • C++ supports both
  • C does not support either

Memory Management

  • C++ new/delete and smart pointers
  • C: calloc() / malloc() and free()

Compiler

reference variable

  • C++ supports the reference variable
  • C doesn't support the reference variable

Functions in Structure

  • C doesn't allow functions in structure
  • C++ allows functions in structure

Use Cases

C++ is more popular with applications level programming. C is more popular in low level systems programming.

  • C is used in
    • operating systems, e.g. Linux Kernel (C++ is not allowed in Linux Kernel)
    • embedded systems.
    • compilers, libraries, interpreters of other languages: CPython (https://github.com/python/cpython)
    • graphics: Vulkan, OpenGL.
    • databases: PostgreSQL, Redis.
  • C++
    • compilers, libraries, interpreters of other languages: OpenJDK, V8
    • game engines: Unity, Unreal Engine.
    • databases: MongoDB, sqlite3
    • server-side programming (used by many companies, like Google, Facebook, etc).
  • many projects use both, e.g. Chrome, MySQL, etc.

Linus Torvalds’ views on C++

C++ is a horrible language. It’s made more horrible by the fact that a lot of substandard programmers use it, to the point where it’s much much easier to generate total and utter crap with it. Quite frankly, even if the choice of C were to do nothing but keep the C++ programmers out, that in itself would be a huge reason to use C.