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 token that functions either as a binary addition operator or a unary plus operator, depending on its arity. It operates on arithmetic types (integer and floating-point) and pointer types, triggering specific type conversions and memory scaling rules defined by the C Standard.

Binary Addition Operator

When used with two operands, + computes the sum of its operands.
expression1 + expression2
The behavior of the binary + operator is strictly dictated by the types of its operands: 1. Arithmetic Addition If both operands possess arithmetic types, the compiler applies the usual arithmetic conversions before evaluating the expression. This process determines a common real type for the operation, evaluated in the following sequence:
  • Floating-Point Hierarchy: Floating-point types are evaluated before integer ranks. If either operand is a floating-point type, the hierarchy is long double > double > float. The operand with the lesser type (or any integer type) is implicitly converted to the greater floating-point type.
  • Integer Promotions: If neither operand is a floating-point type, integer promotions are applied to both operands. Types smaller than int (e.g., char, short) are promoted to int or unsigned int.
  • Integer Conversion Ranks and Signedness: After promotions, if the integer types still differ, the compiler resolves the common type based on integer conversion rank and signedness:
    1. If both operands have the same signedness (both signed or both unsigned), the operand with the lower integer conversion rank is converted to the type with the higher rank.
    2. If the unsigned operand has a rank greater than or equal to the signed operand, the signed operand is converted to the unsigned operand’s type.
    3. If the signed operand has a strictly greater rank and can represent all possible values of the unsigned operand’s type, the unsigned operand is converted to the signed operand’s type.
    4. If the signed operand has a strictly greater rank but cannot represent all values of the unsigned operand’s type, both operands are converted to the unsigned type corresponding to the signed operand’s type.
2. Pointer Arithmetic If one operand is a pointer to a complete object type and the other is an integer type, the + operator performs pointer arithmetic.
  • The integer operand is implicitly multiplied by the size (in bytes) of the type the pointer points to (sizeof(*pointer)).
  • The result is a new pointer of the same type, offset by the scaled integer value.
  • Constraint: It is a constraint violation to add two pointers together. The + operator only supports pointer + integer or integer + pointer.
  • Constraint: Pointer arithmetic is invalid on pointers to incomplete types (e.g., void*) or function pointers, as their size is not defined by the standard.

Unary Plus Operator

When used with a single operand, + yields the value of its operand after applying type promotions.
+expression
  • The operand must have an arithmetic type.
  • The compiler applies integer promotions to the operand. For example, applying unary + to a uint8_t will result in an int (assuming int can represent all values of uint8_t on the target architecture).
  • Unlike the unary - operator, unary + does not alter the sign or mathematical value of the operand; its primary mechanical effect is enforcing integer promotion.

Undefined Behavior (UB)

The + operator can invoke undefined behavior under specific conditions:
  • Signed Integer Overflow: If the result of an addition between signed integers cannot be represented in the resulting type, the behavior is undefined. (Note: Unsigned integer addition does not overflow; it wraps around modulo 2n2^n, where nn is the number of bits in the result type).
  • Pointer Bounds Violation: When performing pointer arithmetic, if the resulting pointer points outside the bounds of the array object the original pointer points to (or further than one element past the end of that array), the behavior is undefined.
  • Floating-Point Exceptions: Depending on the floating-point environment (<fenv.h>), adding floating-point numbers that result in overflow, underflow, or NaN (Not a Number) may raise hardware exceptions.
Master C with Deep Grasping Methodology!Learn More