C Assignment Operators – Full Guide with Examples for Beginners
In C programming, assignment operators are used to assign values to variables. They’re essential for storing results and updating values during program execution.
What is an Assignment Operator?
An assignment operator assigns a value to a variable. It’s commonly used in expressions and loops, and is essential for updating variables.
Basic Assignment Operator
Operator
Description
Example
=
Assign value
x = 10;
int a;
a = 5; // assigns the value 5 to variable a
Compound Assignment Operators in C
These operators combine basic arithmetic with assignment to shorten the code.
Operator
Meaning
Equivalent To
+=
Add and assign
x = x + y
-=
Subtract and assign
x = x - y
*=
Multiply and assign
x = x * y
/=
Divide and assign
x = x / y
%=
Modulo and assign
x = x % y
Examples of Assignment Operators in Action
#include <stdio.h>
int main() {
int a = 10;
a += 5; // a = a + 5 = 15
printf("a += 5: %d\n", a);
a -= 3; // a = a - 3 = 12
printf("a -= 3: %d\n", a);
a *= 2; // a = a * 2 = 24
printf("a *= 2: %d\n", a);
a /= 4; // a = a / 4 = 6
printf("a /= 4: %d\n", a);
a %= 4; // a = a % 4 = 2
printf("a %%= 4: %d\n", a);
return 0;
}
Output:
a += 5: 15 a -= 3: 12 a *= 2: 24 a /= 4: 6 a %= 4: 21