How to Use Assignment Operators in C: Syntax and Examples
In C,
= is the basic assignment operator used to store a value in a variable. You can also use compound assignment operators like +=, -=, *=, and /= to update a variable's value by combining an operation with assignment.Syntax
The basic syntax for assignment operators in C is variable operator= expression;. Here, variable is where the value is stored, operator= is the assignment operator, and expression is the value or calculation assigned.
Common assignment operators include:
=: Assigns the value on the right to the variable on the left.+=: Adds the right value to the variable and assigns the result.-=: Subtracts the right value from the variable and assigns the result.*=: Multiplies the variable by the right value and assigns the result./=: Divides the variable by the right value and assigns the result.%=: Takes the modulus of the variable by the right value and assigns the result.
c
int a = 5; // Basic assignment a += 3; // Equivalent to a = a + 3; a -= 2; // Equivalent to a = a - 2; a *= 4; // Equivalent to a = a * 4; a /= 2; // Equivalent to a = a / 2; a %= 3; // Equivalent to a = a % 3;
Example
This example shows how to use different assignment operators to update a variable's value step-by-step.
c
#include <stdio.h> int main() { int num = 10; printf("Initial value: %d\n", num); num += 5; // num = 10 + 5 = 15 printf("After += 5: %d\n", num); num -= 3; // num = 15 - 3 = 12 printf("After -= 3: %d\n", num); num *= 2; // num = 12 * 2 = 24 printf("After *= 2: %d\n", num); num /= 4; // num = 24 / 4 = 6 printf("After /= 4: %d\n", num); num %= 5; // num = 6 % 5 = 1 printf("After %%= 5: %d\n", num); return 0; }
Output
Initial value: 10
After += 5: 15
After -= 3: 12
After *= 2: 24
After /= 4: 6
After %= 5: 1
Common Pitfalls
One common mistake is confusing the assignment operator = with the equality operator ==. Using = inside conditions can cause bugs.
Another pitfall is dividing by zero when using /=, which causes runtime errors.
c
#include <stdio.h> int main() { int x = 5; // Wrong: using assignment instead of comparison if (x = 0) { printf("This will never run because x is assigned 0 and evaluates to false.\n"); } else { printf("This will always run.\n"); } // Correct: if (x == 0) { printf("x is zero.\n"); } else { printf("x is not zero.\n"); } // Danger: dividing by zero int y = 10; int z = 0; // y /= z; // Uncommenting this will cause a runtime error (division by zero) return 0; }
Output
This will always run.
x is not zero.
Quick Reference
| Operator | Meaning | Example |
|---|---|---|
| = | Assign value | a = 5; |
| += | Add and assign | a += 3; // a = a + 3 |
| -= | Subtract and assign | a -= 2; // a = a - 2 |
| *= | Multiply and assign | a *= 4; // a = a * 4 |
| /= | Divide and assign | a /= 2; // a = a / 2 |
| %= | Modulus and assign | a %= 3; // a = a % 3 |
Key Takeaways
Use
= to assign values and compound operators like += to update variables efficiently.Never confuse
= (assignment) with == (comparison) in conditions.Avoid dividing by zero when using
/= to prevent runtime errors.Compound assignment operators combine arithmetic and assignment in one step for cleaner code.
Always initialize variables before using assignment operators to avoid undefined behavior.