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.
int total = 0; total += 5; /* 5 */ total *= 3; /* 15 */
Try it
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.
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
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 = cassignsctobfirst. =is not==.if (x = 5)assigns and is always true. GCC warns with-Wallunless you add an extra pair of parentheses to show it is intentional:if ((x = next())).- Compound assignment evaluates the left side once.
a[i++] += 2incrementsionce, whereasa[i++] = a[i++] + 2would be a bug. - Implicit conversion happens on the result:
int n = 10; n *= 1.5;computes indouble, then truncates back toint.-Wconversionwarns 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
memcpyor loop. (Wrapping an array in a struct makes it copyable.) - Initialization is not assignment.
int x = 5;initializes;constobjects can only be initialized. - Shifts need valid counts:
x <<= nwithnnegative or at least the type's width is undefined behavior. - Assigning a pointer to another copies the address, not the data it points to.