Assignment Operators in C/C++ - GeeksforGeeks

11 Oct, 2019
Assignment operators are used to assigning value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value. The value on the right side must be of the same data-type of the variable on the left side otherwise the compiler will raise an error.Different types of assignment operators are shown below:
“=”: This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left.For example:a = 10;
b = 20;
ch = 'y';

a = 10;
b = 20;
ch = 'y';

“+=”: This operator is combination of ‘+’ and ‘=’ operators. This operator first adds the current value of the variable on left to the value on the right and then assigns the result to the variable on the left.Example:(a += b) can be written as (a = a + b)
If initially value stored in a is 5. Then (a += 6) = 11.
(a += b) can be written as (a = a + b)

If initially value stored in a is 5. Then (a += 6) = 11.
“-=”This operator is combination of ‘-‘ and ‘=’ operators. This operator first subtracts the current value of the variable on left from the value on the right and then assigns the result to the variable on the left.Example:(a -= b) can be written as (a = a - b)
If initially value stored in a is 8. Then (a -= 6) = 2.
(a -= b) can be written as (a = a - b)

If initially value stored in a is 8. Then (a -= 6) = 2.
“*=”This operator is combination of ‘*’ and ‘=’ operators. This operator first multiplies the current value of the variable on left to the value on the right and then assigns the result to the variable on the left.Example:(a *= b) can be written as (a = a * b)
If initially value stored in a is 5. Then (a *= 6) = 30.
(a *= b) can be written as (a = a * b)

If initially value stored in a is 5. Then (a *= 6) = 30.
“/=”This operator is combination of ‘/’ and ‘=’ operators. This operator first divides the current value of the variable on left by the value on the right and then assigns the result to the variable on the left.Example:(a /= b) can be written as (a = a / b)
If initially value stored in a is 6. Then (a /= 2) = 3.
(a /= b) can be written as (a = a / b)

If initially value stored in a is 6. Then (a /= 2) = 3.
Below example illustrates the various Assignment Operators:
C
C++
// C program to demonstrate// working of Assignment operators  #include <stdio.h>  int main(){      // Assigning value 10 to a    // using "=" operator    int a = 10;    printf("Value of a is %d\n", a);      // Assigning value by adding 10 to a    // using "+=" operator    a += 10;    printf("Value of a is %d\n", a);      // Assigning value by subtracting 10 from a    // using "-=" operator    a -= 10;    printf("Value of a is %d\n", a);      // Assigning value by multiplying 10 to a    // using "*=" operator    a *= 10;    printf("Value of a is %d\n", a);      // Assigning value by dividing 10 from a    // using "/=" operator    a /= 10;    printf("Value of a is %d\n", a);      return 0;}
// C++ program to demonstrate // working of Assignment operators   #include <iostream>using namespace std;  int main() {       // Assigning value 10 to a     // using "=" operator     int a = 10;     cout << "Value of a is "<<a<<"\n";       // Assigning value by adding 10 to a     // using "+=" operator     a += 10;     cout << "Value of a is "<<a<<"\n";       // Assigning value by subtracting 10 from a     // using "-=" operator     a -= 10;     cout << "Value of a is "<<a<<"\n";       // Assigning value by multiplying 10 to a     // using "*=" operator     a *= 10;     cout << "Value of a is "<<a<<"\n";       // Assigning value by dividing 10 from a     // using "/=" operator     a /= 10;     cout << "Value of a is "<<a<<"\n";       return 0; }
Value of a is 10
Value of a is 20
Value of a is 10
Value of a is 100
Value of a is 10

C Basics
C-Operators
CPP-Basics
cpp-operator
C Language
cpp-operator
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
TCP Server-Client implementation in C
Exception Handling in C++
'this' pointer in C++
Multithreading in C
Arrow operator -> in C/C++ with Examples
Multiple Inheritance in C++
Smart Pointers in C++ and How to Use Them
Understanding "extern" keyword in C
UDP Server-Client implementation in C
Ways to copy a vector in C++