C vs C++#
Type and Tag Namespaces#
In C and C++, user-defined types are placed into different compiler namespaces, affecting whether type keywords can be omitted.
| Type | C Syntax | C++ Syntax |
|---|---|---|
struct | struct A a; (requires typedef to omit) | A a; |
union | union U u; (requires typedef to omit) | U u; |
enum | enum Color c; (requires typedef to omit) | Color c; |
class | Not supported | MyClass obj; |
C: Separate Tag and Identifier Namespaces#
- In C, names of
struct,union, andenumreside in a dedicated tag namespace, distinct from ordinary identifiers. - Without a
typedef, variable declarations must include the defining keyword:struct Node { int val; }; struct Node n; // 'struct' keyword required in C
C++: Unified Type Namespace#
- In C++, names introduced by
struct,class,union, orenumautomatically enter the type namespace. - The type name can be used directly without a
typedefor prefix keyword:struct Node { int val; }; Node n; // Valid in C++ without typedef
String Literal Typing#
| Language | Literal Type | char *s = "hello"; | Modification Attempt |
|---|---|---|---|
| C | char[] | Valid (Legacy compatibility) | Runtime crash (Segmentation fault) |
| C++ | const char[] | Compile Error | Prevented at compile time |
- C: String literals are stored in read-only memory, but their formal type is non-const
char[]. Modifying a literal triggers undefined behavior at runtime. - C++: String literals have type
const char[](decaying toconst char *). The compiler prevents modifications and invalid assignments at compile time (enforcing low-level const).
References vs. Pointers#
C++ introduces references (&), which do not exist in C.
- Non-reseatable Binding: A reference
T &ref = obj;is semantically equivalent to a constant pointerT *const ptr = &obj;(cannot be rebound). - Reference to const: A const reference
const T &ref = obj;corresponds semantically toconst T *const ptr = &obj;(neither the address binding nor the target value can be modified). - Object Status: A pointer is an independent object with its own memory address. A reference is an alias and does not occupy an independent address.
lvalues and rvalues in C vs. C++#
The concept of an lvalue originated in C as an expression denoting an object in storage (capable of appearing on the left side of an assignment). While both languages share this foundation, C++ defines operator result categories differently and extends value categories with move semantics.
| Operator / Expression | C Result Category | C++ Result Category | Behavioral Difference |
|---|---|---|---|
Assignment (a = b) | rvalue | lvalue | a = b = 10; (a = (b = 10)) works in both C and C++ because right-associativity puts (b = 10) on the RHS, whereas (a = b) = 10; places the result on the LHS—a compile error in C, but valid in C++ (assigns 10 to a). |
Conditional (cond ? a : b) | Always rvalue | lvalue (if both operands are lvalues) | ((a < b) ? a : b) = 30; is an error in C, but valid in C++ (assigns 30 to a). |
String Literal ("hello") | Non-modifiable lvalue (char[]) | lvalue (const char[]) | Modifying a literal triggers runtime undefined behavior in C, but is rejected at compile time in C++. |
See Expressions for C++11 value categories (xvalue, prvalue, glvalue).