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.

TypeC SyntaxC++ Syntax
structstruct A a; (requires typedef to omit)A a;
unionunion U u; (requires typedef to omit)U u;
enumenum Color c; (requires typedef to omit)Color c;
classNot supportedMyClass obj;

C: Separate Tag and Identifier Namespaces#

  • In C, names of struct, union, and enum reside 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, or enum automatically enter the type namespace.
  • The type name can be used directly without a typedef or prefix keyword:
    struct Node {
        int val;
    };
    Node n; // Valid in C++ without typedef
    

String Literal Typing#

LanguageLiteral Typechar *s = "hello";Modification Attempt
Cchar[]Valid (Legacy compatibility)Runtime crash (Segmentation fault)
C++const char[]Compile ErrorPrevented 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 to const 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 pointer T *const ptr = &obj; (cannot be rebound).
  • Reference to const: A const reference const T &ref = obj; corresponds semantically to const 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 / ExpressionC Result CategoryC++ Result CategoryBehavioral Difference
Assignment (a = b)rvaluelvaluea = 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 rvaluelvalue (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).


# C C++