In this tutorial, you will learn about assignment operators in C, how they are used to assign values to variables, and how shorthand assignment operators simplify code. You will also explore each type of assignment operator with syntax, examples, and output to solidify your understanding.
What are Assignment Operators in C?
In C programming, assignment operators are used to assign values to variables. The most basic assignment operator is the equals sign =, which assigns the value on the right to the variable on the left.
For example:
int x; x = 30; // Assigns 30 to variable x
Apart from the simple assignment, C provides shorthand assignment operators (also called compound assignment operators) that combine arithmetic or bitwise operations with assignment.
Types of Assignment Operators in C
Here’s a list of commonly used assignment operators in C:
Operator | Description | Example | Equivalent To |
---|---|---|---|
= | Simple assignment | x = 5 | — |
+= | Add and assign | x += 5 | x = x + 5 |
-= | Subtract and assign | x -= 3 | x = x – 3 |
*= | Multiply and assign | x *= 2 | x = x * 2 |
/= | Divide and assign | x /= 4 | x = x / 4 |
%= | Modulus and assign | x %= 3 | x = x % 3 |
&= | Bitwise AND and assign | x &= 1 | x = x & 1 |
|= | Bitwise OR and assign | x |= 2 | x = x | 2 |
^= | Bitwise XOR and assign | x ^= 3 | x = x ^ 3 |
<<= | Left shift and assign | x <<= 1 | x = x << 1 |
>>= | Right shift and assign | x >>= 2 | x = x >> 2 |
Simple Assignment (=)
The = operator is used to assign a value to a variable.
#include <stdio.h> int main() { int score; score = 70; printf("C Quiz Score: %d", score); return 0; }
Output:
C Quiz Score: 70
This C program shows how to print a score. It begins by adding the stdio.h library so the program can use the printf function. In the main part, a number variable called score is made and given the value 70. Then, the program prints “C Quiz Score: 70” on the screen. The line return 0; means the program ran without any problems.
Add and Assign (+=)
The += operator adds a value to a variable and then assigns the result back to that same variable. It’s a shorthand for writing x = x + value.
#include <stdio.h> int main() { int marks = 50; // Declare and initialize 'marks' with 50 marks += 20; // Add 20 to 'marks' → marks = marks + 20 printf("Total Marks: %d", marks); // Display the updated value return 0; }
Output:
Total Marks: 70
Subtract and Assign (-=)
The -= operator subtracts a value from a variable and then stores the result back in that same variable. It’s a shorthand for writing x = x – value.
#include <stdio.h> int main() { int balance = 100; balance -= 30; printf("Remaining Balance: %d", balance); return 0; }
Output:
Remaining Balance: 70
This C program shows how to subtract a value from a number. It starts with the stdio.h library so it can use printf. In the main part, a number called balance is set to 100. Then, 30 is taken away from it using balance -= 30;. After that, the program prints “Remaining Balance: 70” to the screen. The return 0; line means the program finished without errors.
Multiply and Assign (*=)
The *= operator multiplies a value with a variable and stores the result back in that variable. It’s a shorthand for writing x = x * value.
#include <stdio.h> int main() { int quantity = 5; quantity *= 4; printf("Total Quantity: %d", quantity); return 0; }
Output:
Total Quantity: 20
Modulus and Assign (%=)
The %= operator finds the remainder when dividing a variable by a value and stores the result back in that variable. It’s a shorthand for writing x = x % value.
#include <stdio.h> int main() { int total = 29; total %= 5; printf("Remainder: %d", total); return 0; }
Output:
Remainder: 4
Bitwise Assignment Operators (&=, |=, ^=)
These operators perform bitwise operations between two values and assign the result back to the original variable.
Bitwise AND and Assign (&=)
The &= operator performs a bitwise AND between two integers and assigns the result back to the variable.
#include <stdio.h> int main() { int a = 12; // Binary: 1100 a &= 10; // 1100 & 1010 = 1000 (which is 8 in decimal) printf("Bitwise AND Result: %d", a); return 0; }
Output:
Bitwise AND Result: 8
Bitwise OR and Assign (|=)
The |= operator performs a bitwise OR between two integers and stores the result back.
#include <stdio.h> int main() { int b = 12; // Binary: 1100 b |= 10; // 1100 | 1010 = 1110 (which is 14 in decimal) printf("Bitwise OR Result: %d", b); return 0; }
Output:
Bitwise OR Result: 14
Bitwise XOR and Assign (^=)
The ^= operator performs a bitwise XOR between two integers and stores the result back.
#include <stdio.h> int main() { int c = 12; // Binary: 1100 c ^= 10; // 1100 ^ 1010 = 0110 (which is 6 in decimal) printf("Bitwise XOR Result: %d", c); return 0; }
Output:
Bitwise XOR Result: 6
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Watch Advanced C Programming Videos
- Practice BCA MCQs
- Check Computer Science Books
- Check C Books
- Apply for C Internship