C programming assignment operators; In this tutorial, you will learn about assignment operators in c programming with examples.
C Assignment Operators
An assignment operator is used for assigning a value to a variable in c programming language.
List of Assignment Operators in C
Note that:- There are 2 categories of assignment operators in C language. Shown below:
- 1. Simple assignment operator ( Example: = )
- 2. Compound assignment operators ( Example: +=, -=, *=, /=, %=, &=, ^= )
Operator | Example | Same as |
---|---|---|
= | a = b | a = b |
+= | a += b | a = a+b |
-= | a -= b | a = a-b |
*= | a *= b | a = a*b |
/= | a /= b | a = a/b |
%= | a %= b | a = a%b |
Example 1 – Assignment Operators
// Working of assignment operators #include <stdio.h> int main() { int a = 5, c; c = a; // c is 5 printf("c = %d\n", c); c += a; // c is 10 printf("c = %d\n", c); c -= a; // c is 5 printf("c = %d\n", c); c *= a; // c is 25 printf("c = %d\n", c); c /= a; // c is 5 printf("c = %d\n", c); c %= a; // c = 0 printf("c = %d\n", c); return 0; }
Output
c = 5 c = 10 c = 5 c = 25 c = 5 c = 0
Note that:- The left side operand of the assignment operator is a variable and the right side operand of the assignment operator is a value. The value on the right side must be of the same data type as the variable on the left side otherwise the compiler will raise an error.