Template Metaprogramming

Are C and C++ the Same?

C and C++ are related languages with separate standards. Learn how they differ in syntax, keywords, the standard library and how code is compiled.

On this page
  1. A short history
  2. Code that compiles in C but not in C++
  3. What C++ adds
  4. Input, output and strings
  5. How the compilers treat them
  6. Which one to learn first
  7. Key differences at a glance

No. C and C++ are related languages, but they are not the same language. C++ began as an extension of C, so much of the syntax looks alike. Each language has its own standard and its own feature set, though, and a few rules conflict. Code written for one does not always compile as the other.

A short history

Bjarne Stroustrup began work on what became C++ at Bell Labs around 1979. The early version was called “C with Classes,” because it added classes to C. The name C++ came in 1983. Since then the two languages have followed separate standards. C has ISO standards such as C99 and C11, and C++ has standards such as C++11 and C++20.

Code that compiles in C but not in C++

Most C code runs as C++ with few changes, but a few differences break compilation. This program is valid C:

int main(void) {
    int class = 1;
    return class;
}

In C++, class is a keyword, so the same code fails to compile. C++ reserves more words than C, including class, template, and namespace. The word new is also reserved in C++ but is an ordinary name in C.

Memory allocation is another difference. In C, malloc returns a void *, which converts to any pointer type without a cast. C++ does not make that conversion implicitly, so the same line needs a cast:

int *numbers = static_cast<int *>(malloc(10 * sizeof(int)));

Many C++ programmers use new instead, which returns a pointer of the right type.

What C++ adds

C++ includes most of C and adds features that C does not have:

  • Classes, with constructors, destructors, and access control.
  • Inheritance and virtual functions for run-time polymorphism.
  • Function and operator overloading.
  • References, which are aliases for existing objects.
  • Templates, which are the basis of c metaprogramming.
  • Namespaces, and exceptions with try and catch.
  • A larger standard library, with containers such as std::vector and std::string.

If classes are new to you, the basics are in what are classes in C++.

Input, output and strings

C prints with printf. C++ offers std::cout through the <iostream> header:

#include <iostream>
#include <string>

int main() {
    std::string name = "Ada";
    std::cout << "Hello, " << name << '\n';
}

C stores text in character arrays that end with a zero byte. C++ offers std::string, which manages its own memory. For more on std::cout, see how to use cout in C++.

How the compilers treat them

The file extension and the compiler command decide which language rules apply. gcc compiles C, and g++ compiles C++. g++ also treats .c files as C++, so compile C code with gcc. C++ source files usually end in .cpp.

C++ changes function names when it compiles them, a process called name mangling. To call a C function from C++ code, the declaration needs an extern "C" block so the names match. Keeping this in mind avoids linker errors when mixing the two.

Which one to learn first

The right starting point depends on your goal. C is smaller, and it shows memory and pointers more directly. C++ is larger, and it is used in game engines and other performance-sensitive software. Since C++ keeps most of C’s syntax, learning C first can help, but it is not required.

The shortcut that C++ is basically C with classes misses a lot, such as templates and the standard library. Whether it is better to use C or C++ depends on the project. Is it better to use C or C++? covers those trade-offs in more detail. For the language on its own, the article What Is C Programming? is a good place to begin.

Key differences at a glance

  • C++ keeps most of C’s syntax, but not all of it.
  • C++ adds classes, references, overloading, templates and namespaces.
  • C++ is stricter about some conversions, such as void *.
  • C and C++ use separate standards, and the file extension and compiler decide the language.

The complete guide to template metaprogramming

Template Metaprogramming in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

stringstream in C++

stringstream in C++ explained: how the <sstream> classes read and write text in memory, convert strings and numbers, split text, and avoid common mistakes.

stringstream 4 min read