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 is a compound assignment operator in C that performs addition and assignment in a single operation. It adds the value of the right operand to the left operand and stores the resulting value in the left operand.
lvalue += rvalue;
Semantically, the expression E1 += E2 is equivalent to E1 = E1 + (E2), with one critical distinction: the left operand (E1) is evaluated exactly once. The parentheses around E2 are mandated by the C standard to preserve operator precedence. Without them, an expression with lower precedence than addition would evaluate incorrectly. For example, x += 1 << 2 evaluates to x = x + (1 << 2) (adding 4), whereas the unparenthesized expansion x = x + 1 << 2 would evaluate as x = (x + 1) << 2.

Operand Constraints and Type Behavior

The behavior of the += operator depends strictly on the data types of its operands: 1. Arithmetic Types If both operands are of arithmetic types (integer or floating-point), the compiler performs the usual arithmetic conversions to determine a common type for the addition. After the addition is computed, the result is implicitly cast back to the type of the lvalue before assignment.
int a = 5;
double b = 3.14;
a += b; // 'a' evaluates to 8. 'b' is added to 'a', and the 8.14 result is truncated to int.
2. Pointer Types If the left operand is a pointer, it must point to a completely defined object type. The right operand must be of an integer type. The operation performs standard pointer arithmetic: the integer value is scaled by the size of the type the pointer points to (sizeof(*lvalue)), and the pointer’s memory address is advanced by that scaled amount.
int arr[5];
int *ptr = arr;
ptr += 3; // Advances the pointer by (3 * sizeof(int)) bytes.

Evaluation and Side Effects

Because the lvalue is evaluated only once, the += operator guarantees safe execution for expressions that mutate state or invoke functions during address resolution.
// The function get_target_pointer() is executed exactly once.
// If expanded to *get_target_pointer() = *get_target_pointer() + (5), 
// the function would be erroneously executed twice.
*get_target_pointer() += 5; 

Return Value, Type, and Associativity

  • Value: The result of a += expression is the new value of the left operand after the assignment has taken place.
  • Type: The type of the evaluated assignment expression is the type the left operand would have after lvalue conversion. Lvalue conversion strips type qualifiers. Therefore, if the left operand is a volatile int, the type of the evaluated expression is int, not volatile int.
  • Associativity: Like all assignment operators in C, += has right-to-left associativity. This allows for chained assignments, where the right-most expression is evaluated and assigned first.
int x = 1, y = 2, z = 3;
x += y += z; 
// Evaluates as: x += (y += z)
// y becomes 5, then x becomes 6.
Master C with Deep Grasping Methodology!Learn More