Skip to content

Differences Between C++ and Other Common Languages

This article introduces the differences between C++ and other commonly used languages, focusing on important or easily overlooked distinctions between C and C++. Although C++ is almost a superset of C, and mixing C/C++ code generally works, understanding the significant differences between C/C++ can help avoid strange bugs. If you are an OIer who primarily uses C, this article will also help you transition to C++ more smoothly. For the unique features added in C++ compared to C, you can read the tutorials in the C++ Advanced section. Additionally, this article briefly introduces the differences between Python, Java, and C++.

Differences Between C and C++

Macros and Templates

One of the initial design purposes of C++ templates was to replace macro definitions. Learning template programming is a crucial step from C to C++. Unlike textual substitution in macros, templates undergo more comprehensive compiler checks during compilation, making it easier to write robust code. The template feature, since C++11, supports variadic template parameters, which can replace C's variadic functions while ensuring type safety.

Pointers and References

In C++, you can still use C-style pointers, but for variable passing, it is recommended to use C++'s reference feature to achieve similar functionality. Since references cannot be null, they can prevent issues related to null pointer access. However, pointers still have their uses due to their flexibility. Notably, the NULL null pointer in C has a type-safe replacement nullptr since C++11. References and pointers can be converted between each other using the * and & operators.

bool

Also refer to Boolean Type.

Unlike C++, the C language originally did not have a Boolean type.

The C99 standard added the _Bool keyword (and the equivalent bool macro) along with the true and false macros. To use the bool, true, and false macros, you need to include the stdbool.h header file. Using _Bool does not require any additional headers.

1
2
bool x = true;  // Requires stdbool.h
_Bool x = 1;    // Does not require stdbool.h

Starting from C23, true, false, and bool become keywords in the C language. Using them no longer requires the stdbool.h header file, while _Bool is retained as an alternative spelling for bool1.

The table below shows the changes in bool type support across different C language standards (with C++ included for comparison):

Language Standard bool true/false _Bool
C89 / / Reserved3
C99 to before C23 Macro, equivalent to _Bool, requires stdbool.h Macro, true equivalent to 1, false equivalent to 0, requires stdbool.h Keyword
Starting from C23 Keyword Keyword Alternative spelling for keyword bool
C++ Keyword Keyword Reserved3

struct

Although both C and C++ have the concept of struct, they are not interchangeable! In C, struct describes a fixed memory layout, while in C++, struct is a class. The only difference from a class is that its members and inheritance behavior are public by default, whereas class members are private by default. This can be particularly critical when writing mixed C/C++ code.

Additionally, declaring a struct in C++ is less verbose than in C. C version:

1
2
3
4
typedef struct Node_t {
  struct Node_t *next;
  int key;
} Node;

C++ version:

1
2
3
4
struct Node {
  Node *next;
  int key;
};

const

In C, const only restricts variables from being modified. In C++, due to the emergence of many new features, const has been given more uses. The successor to const in C is constexpr in C++. For the usage of const in C++, refer to the Constants page.

Memory Allocation

C++ introduces the new and delete keywords to allocate space in the "free store," which can be either the heap or static storage. They are designed to work with "classes." delete[] can also directly release memory for dynamic arrays, which is very convenient. The new and delete keywords call the constructor and destructor of the type. Compared to C's malloc(), realloc(), and free() functions, they provide better type support but are less efficient.

In short, if the objects you need to allocate dynamically are of fundamental types or their arrays, you can use malloc() for more efficient memory allocation. However, if the objects are of non-fundamental types, it is recommended to use new for safety checks. Note that although both new and malloc() return pointers, pointers created with new must be reclaimed with delete, and pointers from malloc() must be reclaimed with free(); otherwise, there is a risk of memory leaks.

Variable Declaration

Before C99, variable declarations in C had to be at the beginning of a block. C++ and C99 onwards have no such restriction.

Variable-Length Arrays

C supports VLAs (Variable-Length Arrays) since C99, while C++ does not support them.

Struct Initialization

Since C99, C supports designated initializers for structs (though optional in C11). C++ only supports designated initializers with ordering requirements since C++20, and does not support C's features like out-of-order, nested, mixed with regular initializers, or array designated initializers2.

Comment Syntax

C++-style single-line comments // are not supported in C before C99.

Differences Between Python and C++

Python is currently the most commonly used language in the machine learning community. Compared to C++, Python's advantages lie in its ease of learning and practice. Python has a simpler and more straightforward syntax, such as not requiring variable type declarations when defining variables. However, this simplicity comes at a cost. Python sacrifices performance compared to C++. C++ is suitable for almost all platforms, including embedded systems, and has faster execution speed, while Python can only be used on platforms that support high-level languages. C++ is closer to the hardware, making it suitable for writing operating systems.

Differences Between Java and C++

Both Java and C++ are object-oriented languages and use object-oriented concepts (encapsulation, inheritance, polymorphism). Due to the many excellent features of object-oriented programming (inheritance, composition, etc.), both have good reusability. Therefore, compared to Python, Java and C++ are more similar.

The biggest difference is that Java has the JVM mechanism. JVM stands for Java Virtual Machine. A very important characteristic of Java is its platform independence, and the use of the Java Virtual Machine is key to achieving this. Generally, high-level languages need to be compiled into different target code to run on different platforms. With the Java Virtual Machine, Java programs do not need to be recompiled when running on different platforms. The Java Virtual Machine shields platform-specific information, allowing Java programs to compile into target code (bytecode) that runs on the JVM and can be executed on multiple platforms without modification.

Because of this feature, Java is often used for developing programs that need to be ported across different platforms. However, since compiling Java programs starts from bytecode, Java's performance is not as good as C++'s.

References


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

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

  3. Both C and C++ specify that identifiers starting with an underscore followed by an uppercase letter are reserved. For details, see https://en.cppreference.com/w/c/language/identifier. ↩↩