Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The + operator in C is a polymorphic arithmetic operator that functions either as a unary operator for integer promotion or as a binary operator for arithmetic addition and pointer offset calculation. Its behavior, precedence, and type constraints depend strictly on its arity and the data types of its operands.

Unary Plus

When used with a single operand, + acts as the unary plus operator.
+ expression
  • Operands: Must have an arithmetic type (integer or floating-point).
  • Mechanics: The operator performs integer promotions on its operand. If the operand is of a type smaller than int (e.g., char or short), it is promoted to int (or unsigned int). The value and sign of the operand remain completely unchanged.
  • Associativity: Right-to-left.
  • Precedence: High (Level 2), sharing precedence with other unary operators like -, !, and ~.

Binary Plus (Arithmetic Addition)

When used with two arithmetic operands, + acts as the binary addition operator.
expression1 + expression2
  • Operands: Both operands must have arithmetic types.
  • Mechanics: The compiler applies the usual arithmetic conversions to establish a common real type before performing the addition. For example, if one operand is an int and the other is a double, the int is implicitly converted to a double before the sum is calculated.
  • Associativity: Left-to-right.
  • Precedence: Medium (Level 4), evaluated after multiplicative operators (*, /, %) and before shift operators (<<, >>).
  • Overflow Behavior:
    • Unsigned Integers: Addition wraps around using modulo arithmetic (2n2^n, where nn is the number of bits in the type). This is well-defined.
    • Signed Integers: Overflow results in Undefined Behavior (UB). The compiler assumes signed overflow never occurs and may optimize accordingly.

Binary Plus (Pointer Arithmetic)

When one operand is a pointer and the other is an integer, + performs pointer arithmetic.
pointer_expression + integer_expression
integer_expression + pointer_expression
  • Operands: One operand must be a pointer to a completely defined object type. The other must be of an integer type. It is invalid to add two pointers together.
  • Mechanics: The integer operand is implicitly multiplied by the size of the type the pointer points to (sizeof(*pointer)). This calculated byte offset is then added to the pointer’s memory address.
  • Type Constraints: Pointer arithmetic is strictly prohibited on void* and function pointers, as their target types have no defined size.
  • Bounds and Undefined Behavior: The resulting pointer must point to an element within the same array object, or exactly one element past the end of the array. If the calculated address falls outside these bounds, the result is Undefined Behavior.
Master C with Deep Grasping Methodology!Learn More