Differences Between C++ and Other Common Languages

This article introduces the differences between C++ and other common languages, focusing on important or easily overlooked differences between C and C++. Although C++ is almost a superset of C and mixing C/C++ code generally causes no problems, understanding important differences between C and C++ can help avoid strange bugs. If C is your primary language as an OIer, this article can also help you get started with C++ more smoothly. For the distinctive features added by C++ over C, see the Advanced C++ tutorials. This article also briefly introduces the differences between Python, Java, and C++.

Differences Between C and C++

Macros and Templates

One of the original purposes of C++ templates was to replace macro definitions. Learning template programming is an important step from C to C++. Unlike the textual replacement performed by macros, templates receive more comprehensive compiler checks at compile time, making it easier to write robust code. Since C++11, templates have supported parameter packs of variable length, which can replace variadic functions in C while ensuring type safety.

Pointers and References

You can still use C-style pointers in C++, but for passing variables, C++'s reference feature is generally preferred for similar purposes. Because a reference cannot refer to a null object, it avoids some invalid-address access problems. Pointers remain useful because of their flexibility. It is worth noting that C's null pointer NULL has a type-safe replacement, nullptr, since C++11. References and pointers can be converted between each other through the * and & operators.

bool

See also Boolean type.

Unlike C++, the C language originally had no Boolean type.

The C99 standard added the _Bool keyword (as well as an equivalent bool macro), together with the true and false macros. To use the three macros bool, true, and false, a program must include the stdbool.h header. Using _Bool does not require any additional header.

bool x = true;  // requires stdbool.h
_Bool x = 1;    // does not require stdbool.h

Starting with C23, true, false, and bool became keywords in C. They no longer require inclusion of stdbool.h, while _Bool remains an alternative spelling for bool1.

The table below shows how support for the bool type changes across C language standards; C++ is included for comparison:

Language standardbooltrue/false_Bool
C89//reserved2
C99 onward, before C23macro equivalent to _Bool, requires stdbool.hmacros; true is equivalent to 1, false to 0; requires stdbool.hkeyword
C23 onwardkeywordkeywordalternative spelling of the bool keyword
C++keywordkeywordreserved2

struct

Although both C and C++ have the concept of struct, the corresponding constructs cannot be used interchangeably. In C, struct describes a fixed memory layout, while in C++ it is a class. Its only difference from a class is that its members and inheritance are public by default, whereas ordinary classes have private members by default. This distinction is especially important when writing mixed C/C++ code.

C++ also does not require the cumbersome syntax that C uses when declaring a struct. The C version is:

typedef struct Node_t {
  struct Node_t *next;
  int key;
} Node;

The C++ version is:

struct Node {
  Node *next;
  int key;
};

const

In C, const only qualifies a variable as non-modifiable. In C++, the emergence of many new features has given const more uses. The C++ successor to C's const is constexpr; for uses of const in C++, see the Constant Expressions page.

Memory Allocation

C++ adds the new and delete keywords to allocate space in the “free store”, which may be the heap or static storage. They were introduced to work with classes. In particular, delete[] can directly release the memory of a dynamic array. The new and delete keywords call constructors and destructors for the type. Compared with C's malloc(), realloc(), and free(), they provide better type support, but are less efficient.

In short, if the object to be allocated dynamically is a fundamental type or an array of fundamental types, malloc() can provide more efficient allocation. If the object being created is a non-fundamental type, new is recommended for type-safety checks. Note that although both new and malloc() return pointers, a pointer returned by new can only be reclaimed with delete, while one returned by malloc() can only be reclaimed with free(); otherwise, memory leaks may result.

Variable Declarations

Before C99, C required variable declarations to appear at the beginning of a block. C++ and C since C99 have no such restriction.

Variable-Length Arrays

Since C99, C supports VLAs (variable-length arrays), while C++ has never supported them.

Struct Initialization

Since C99, C has supported designated initializers for structs (although they are optional in C11). C++ did not support designated initializers with ordering requirements until C++20, and C++ does not support C's out-of-order, nested, mixed-with-ordinary-initializer, or array designated-initializer features3.

Comment Syntax

C++-style single-line comments // were not supported by C before C99.

Differences Between Python and C++

Python is currently the most commonly used language in machine learning. Compared with C++, Python is easier to learn and practice. Its syntax is simpler and more direct; for example, variable types do not need to be declared in advance. This simplicity comes at a cost: Python sacrifices performance compared with C++. C++ runs on almost every platform, including embedded systems, and executes faster, whereas Python can only be used on platforms that support high-level languages. C++ is closer to the hardware and can therefore be used to write operating systems.

Differences Between Java and C++

Both Java and C++ are object-oriented languages and use object-oriented ideas such as encapsulation, inheritance, and polymorphism. Because object orientation provides useful features such as inheritance and composition, both languages have good reusability. Thus, Java and C++ are more similar to each other than either is to Python.

The biggest difference is Java's JVM mechanism. JVM stands for Java Virtual Machine. An important feature of Java is platform independence, and using the Java virtual machine is the key to achieving it. A general high-level language must usually be compiled into different target code for different platforms. With the Java virtual machine, Java programs do not need to be recompiled for different platforms. The JVM hides platform-specific information, so a Java compiler only needs to generate target code (bytecode) for the JVM, which can run on multiple platforms without modification.

Because of this feature, Java is often used to develop programs that must be ported to different platforms. However, because Java programs begin execution from bytecode, Java is not as fast as C++.

References

Footnotes

  1. https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3054.pdf. ↩

  2. Both C and C++ specify that an identifier beginning with an underscore followed by an uppercase letter is reserved; see https://en.cppreference.com/w/c/language/identifier. ↩ ↩2

  3. https://en.cppreference.com/w/cpp/language/aggregate_initialization ↩