Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Operators and Expressions

Assignment operators in C

Reference for C assignment operators: simple = and compound forms such as +=, <<= and |=, assignment as an expression, chained assignment and the = versus == trap.

The assignment operator = stores the value of its right operand in the object named by its left operand. Unlike in many languages, an assignment is an expression: it has a value (the value now stored in the left operand, converted to its type) and so it can appear inside a condition or in a chain.

C also offers compound assignment operators, one for each binary arithmetic and bitwise operator: +=, -=, *=, /=, %=, &=, |=, ^=, <<= and >>=. x += y means x = x + y, except that x is evaluated only once.

C
int total = 0;
total += 5;      /* 5 */
total *= 3;      /* 15 */

Try it

C Simple and compound assignment

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Assignment inside conditions, and the == trap

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
lvalue = expression
lvalue op= expression      /* op is one of * / % + - << >> & ^ | */
Operator Equivalent to
x = y store y (converted) into x
x += y, x -= y x = x + y, x = x - y
x *= y, x /= y, x %= y multiply, divide, remainder
x &= y, x |= y, x ^= y bitwise AND, OR, XOR
x <<= y, x >>= y left and right shift

Operand rules: the left operand must be a modifiable lvalue (not const, not an array, not a function). Result: the new value of the left operand, but not an lvalue in C.

Errors / undefined behavior: assigning to an object whose value is also modified elsewhere in the same expression (a[i] = i++) is undefined or unspecified. Overflow in += on signed types is undefined; assigning an out-of-range value to a signed type is implementation-defined.

Usage notes

  • Associativity is right to left: a = b = c assigns c to b first.
  • = is not ==. if (x = 5) assigns and is always true. GCC warns with -Wall unless you add an extra pair of parentheses to show it is intentional: if ((x = next())).
  • Compound assignment evaluates the left side once. a[i++] += 2 increments i once, whereas a[i++] = a[i++] + 2 would be a bug.
  • Implicit conversion happens on the result: int n = 10; n *= 1.5; computes in double, then truncates back to int. -Wconversion warns about it.
  • Struct and union assignment copies all members, including padding bits that you must not rely on. Arrays cannot be assigned; copy them with memcpy or loop. (Wrapping an array in a struct makes it copyable.)
  • Initialization is not assignment. int x = 5; initializes; const objects can only be initialized.
  • Shifts need valid counts: x <<= n with n negative or at least the type's width is undefined behavior.
  • Assigning a pointer to another copies the address, not the data it points to.