In the C and C++ programming languages, operators are special symbols or punctuation that denote computations, data manipulation, or control structures applied to one or more operands. They are defined by the language specifications (ISO/IEC 9899 for C and ISO/IEC 14882 for C++) and are integral to expression syntax, operator precedence, and associativity rules.
Classification of Operators
| Category | C and C++ Operators | Typical Operands | Description |
|---|---|---|---|
| Arithmetic | + - * / % |
Arithmetic types (integral, floating‑point) | Perform addition, subtraction, multiplication, division, and remainder operations. |
| Increment / Decrement | ++ -- |
Modifiable lvalues of arithmetic or pointer type | Pre‑ or post‑increment/decrement the operand. |
| Relational | < > <= >= |
Ordered types (numeric, pointer) | Yield a Boolean result indicating the relational ordering of operands. |
| Equality | == != |
Any comparable types | Test for equality or inequality. |
| Logical | && ` |
!` |
|
| Bitwise | & ` |
^ ~ << >>` |
Integral types |
| Assignment | = += -= *= /= %= <<= >>= &= ^= ` |
=` | Modifiable lvalues |
| Conditional (ternary) | ?: |
Any types (subject to conversion) | Selects one of two expressions based on a Boolean condition. |
| Comma | , |
Any type | Evaluates left operand, discards its result, then evaluates right operand; the result is the right operand. |
| Member Access | . -> |
Structures, unions, classes, pointers to them | Access members of an object or through a pointer. |
| Pointer | * (indirection) & (address‑of) |
Objects, functions | Obtain the address of an operand or dereference a pointer. |
| Sizeof | sizeof |
Any type or expression | Yields the size (in bytes) of its operand. |
| Cast | (type) |
Any expression | Performs an explicit conversion to the specified type. |
| Alignment | _Alignof (C11) alignof (C++) |
Types | Yields the alignment requirement of a type. |
| Other (C++) | new delete new[] delete[] :: typeid dynamic_cast static_cast reinterpret_cast const_cast .* ->* [] (subscript) |
Various | Provide dynamic memory management, scope resolution, runtime type information, and explicit conversion mechanisms unique to C++. |
| User‑defined operators | Overloadable operators (all except . ?: :: sizeof typeid alignof) |
Class types | In C++, operators can be overloaded as member functions or non‑member functions to define custom semantics for class objects. C does not support operator overloading. |
Operator Precedence and Associativity
Both languages define a fixed hierarchy governing the order in which operators are evaluated when parentheses are omitted. Precedence levels (from highest to lowest) include:
- Postfix:
(),[],->,.,++,--(left‑to‑right) - Unary:
+,-,!,~,*(indirection),&(address‑of),sizeof,++,--,new,delete,typeid,alignof(right‑to‑left) - Multiplicative:
*,/,%(left‑to‑right) - Additive:
+,-(left‑to‑right) - Shift:
<<,>>(left‑to‑right) - Relational:
<,>,<=,>=(left‑to‑right) - Equality:
==,!=(left‑to‑right) - Bitwise AND:
&(left‑to‑right) - Bitwise XOR:
^(left‑to‑right) - Bitwise OR:
|(left‑to‑right) - Logical AND:
&&(left‑to‑right) - Logical OR:
||(left‑to‑right) - Conditional:
?:(right‑to‑left) - Assignment:
=,+=,-=,*=,/=,%=…|=(right‑to‑left) - Comma:
,(left‑to‑right)
The exact tables are given in the language standards and are reproduced in many reference manuals.
Differences Between C and C++
| Aspect | C (ISO/IEC 9899) | C++ (ISO/IEC 14882) |
|---|---|---|
| Operator Overloading | Not permitted. | Allowed for most operators; defined by user‑provided functions. |
| Additional Operators | None beyond the core set. | new, delete, new[], delete[], :: (scope resolution), typeid, dynamic_cast, static_cast, reinterpret_cast, const_cast, .*, ->*, [] (subscript) (the subscript operator already exists in C, but can be overloaded in C++). |
| Boolean Context | Boolean results are integer values (0 or non‑zero). |
Dedicated bool type with true/false. |
| Logical Operators on Pointers | May be used, yielding integer truth values. | Same semantics, but results are of type bool. |
| Shift Operators on Signed Types | Implementation‑defined for negative operands. | Same rule; C++ also defines overloads for class types. |
sizeof |
Compile‑time operator returning size_t. |
Same, but may be applied to expressions of class type yielding the size of the class after overloads of operator sizeof (a non‑standard extension, not part of ISO C++). |
_Alignof / alignof |
_Alignof introduced in C11. |
alignof is part of C++11 onward. |
?: Conditional Operator |
Requires the second and third operands to have compatible types; may perform conversions. | Same rules, with additional overloads possible via conversion functions. |
Usage Guidelines
- Precedence Awareness: Relying on precedence can obscure intent; parentheses are recommended for complex expressions.
- Side Effects: Operators such as
++,--, assignment, and the comma operator produce side effects. The order of evaluation of sub‑expressions (except where sequence points are defined in C or sequencing rules in C++) can affect program behavior. - Type Safety: C++’s type‑rich
booland explicit cast operators (static_cast,reinterpret_cast, etc.) provide stronger compile‑time checks than C’s implicit conversions. - Operator Overloading: Overloading should preserve the conventional meaning of an operator to avoid reducing code readability.
- Standard Compliance: Code intended for portability should conform to the ISO specifications for the target language version (e.g., C99, C11, C++17, C++20).
References
- ISO/IEC 9899:2018 (C18) – Programming language C.
- ISO/IEC 14882:2020 (C++20) – Programming language C++.
- Bjarne Stroustrup, The C++ Programming Language, 4th ed., Addison‑Wesley, 2013.
- Dennis Ritchie, The C Programming Language, 2nd ed., Prentice Hall, 1988.