← Back to C Course | Chapter 3: Operators | Lesson 6 of 9

C Assignment Operators

Assignment operators put a value in a variable, and the short versions update it using its old value, like 'add 5 to my score'. They save you repeating the name.
Syntax
c
variable = value;
variable += value;  // also -= *= /= %=

Simple Assignment (=)

= stores the value on its right side into the variable on its left -- the most basic operation in C, and distinct from == which compares two values instead of assigning one, a mix-up that causes many subtle bugs.

Example: Simple Assignment (=)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int x;
	x = 10;
	// Print formatted text to the screen
	printf("%d", x);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Add (+=) and Subtract (-=) Shorthands

+= and -= are shorthand for adding or subtracting a value and immediately storing the result back into the same variable, so total += 5; is exactly equivalent to total = total + 5; but shorter to write and read.

Example: Add (+=) and Subtract (-=) Shorthands

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare total and set it to 10
	int total = 10;
	total += 5;
	total -= 2;
	// Print formatted text to the screen
	printf("%d", total);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Multiply (*=) and Divide (/=) Shorthands

*= and /= work the same shorthand way for multiplication and division -- price *= 1.1; applies a 10% increase to price in place, without needing to repeat the variable name on both sides of the expression.

Example: Multiply (*=) and Divide (/=) Shorthands

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare price and set it to 100
	float price = 100;
	price *= 1.1f;
	// Print formatted text to the screen
	printf("%.2f", price);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Modulo Assignment (%=)

%= divides the variable by a value and stores the remainder back into that same variable -- useful for things like keeping a rotating counter within a fixed range, such as wrapping an index back to 0 after reaching an array's size.

Example: Modulo Assignment (%=)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare index and set it to 7
	int index = 7;
	index %= 5;
	// Print formatted text to the screen
	printf("%d", index);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Bitwise Assignment Shorthands

C also provides &=, |=, ^=, <<=, and >>= as shorthand for applying a bitwise operation and reassigning the result in one step -- the same read-modify-write pattern as += and -=, just for bit-level operations instead of arithmetic.

Example: Bitwise Assignment Shorthands

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare flags and set it to 6
	int flags = 6;
	flags &= 3;
	// Print formatted text to the screen
	printf("%d", flags);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Writing x =+ 5 when you mean x += 5, which assigns +5 to x instead of adding 5.
  2. Using /= with int variables and expecting decimals, so x /= 2 with x equal to 5 gives 2.
  3. Confusing = (assignment) with == (comparison), for example if (x += 1) instead of a real test.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.