Assignment Operators
Assignment Statement
An assignment statement is a statement that assigns a value to a variable.

Simple Assignment

  • variable = expression;
  • The expression is evaluated and the result is assigned to the variable
Operator Operation Example Result
= Assignment x = y Assign x the value of y
Table 1

The simple assignment operator "=", assigns the value on its right to the variable on its left.

Compound Assignment

  • variable = variable op expression;
  • Statements with the same variable on each side of the equals sign
  • May use the shortcut assignment operators (compound assignment)
Operator Operation Example Result
+= Compound
Assignment
x += y x = x + y
-= Compound
Assignment
x -= y x = x - y
*= Compound
Assignment
x *= y x = x * y
/= Compound
Assignment
x /= y x = x / y
%= Compound
Assignment
x %= y x = x % y
&= Compound
Assignment
x &= y x = x & y
^= Compound
Assignment
x ^= y x = x ^ y
|= Compound
Assignment
x |= y x = x | y
«= Compound
Assignment
x <<= y x = x << y
»= Compound
Assignment
x >>= y x = x >> y
Table 2

When writing any kind of program, there will be many instances where you will need to use a variable as part of an expression and you will want to assign the result of that expression into the same variable. An example of this kind of operation is when you want to increment a variable by some value (variable or constant). For example, if we wanted to add 5 to a variable called x, we could write it as x = x + 5 since we want to add 5 to the value of x and store the result back in x.

Example

Because it is extremely common to have the same variable on both sides of the expression, C provides a shortcut notation. This is implemented with one of a series of compound assignment operators. Using the same example, if we wanted to add 5 to the variable x, we could write it as x += 5. Similar operations may be carried out using one of the many compound operators shown in Table 2.

Example

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.