logo

C vs C++

Last Updated: 2022-03-20

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

  • C++: every 3 years (C++98, C++03, C++11, C++14, C++17, C++20, C++23)
  • C: updated much slower than C++ (C99, C11, C17, C23)

Procedural vs Object-oriented

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

C headers vs C++ headers

  • C Header: with .h suffix, e.g. <time.h>
  • C++ Header: with c prefix, e.g. <ctime>

Differences:

  • C does not have namespace. C headers will go into the global namespace, C++ style headers will go into std namespace
  • C headers don't have overloaded functions, e.g. C++ header <cmath> has overloaded sqrt functions for int and for float, but <math.h> only has one .

Some C headers do not have C++ version, e.g. <stdatomic.h>.

Takeaway: C++ program should avoid any C-Style headers <xxxx.h> if possible. (e.g. use cstdio instead of stdio.h):

#include <cstdio>

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

Overloading / Overriding

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

Memory Management

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

Read more: C / C++ Memory Management

Security

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

Type cast / conversion

  • C-style cast: conversion (int)3.5; cast (int)"hello"
  • C++-style cast: static_cast, etc.

Strings

  • C-style string: char*
  • C++-style string: std::string and std::string_view (since C++17)

Build tools

Both can use Clang and GCC.

They have different standard libraries:

  • C: libc. Implementations: glibc (GNU C Library), musl(a lightweight implementation for Linux)
  • C++: libc++ in Clang, libstdc++ in GCC.

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.