Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
Assignment Operators in C++
Next article icon

Assignment Operators in C++

Last Updated : 08 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, the assignment operator forms the backbone of computational processes by performing a simple operation like assigning a value to a variable. It is denoted by equal sign ( = ) and provides one of the most basic operations in any programming language i.e. assign some value to the variables in C++ or in other words, it is used to store some kind of information.

Example

C++
#include <iostream> using namespace std;  int main() {     int x;        // Assign the value 20 to variable x using assignment     // operator     x = 20;     cout << x;     return 0; } 

Output
20

Explanation: In this program, the assignment operator (=) is used to assign the value 20 to the variable x. It stores the value on the right-hand side (20) into the variable on the left-hand side (x) for further use in the program.

Syntax

variable = value;

The right-hand side value will be assigned to the variable on the left-hand side. The variable and the value should be of the same data type. The value can be a literal or another variable of the same data type.

Compound Assignment Operators

In C++, the assignment operator can be combined into a single operator with some other operators to perform a combination of two operations in one single statement. These operators are called Compound Assignment Operators. There are 10 compound assignment operators in C++:

OperatorDescriptionExample
+=Add and assigna += b;
-=Subtract and assigna -= b;
*=Multiply and assigna *= b;
/=Divide and assigna /= b;
%=Modulus and assigna %= b;
&=Bitwise AND and assigna &= b;
|=Bitwise OR and assigna |= b;
^=Bitwise XOR and assigna ^= b;
<<=Left shift and assigna <<= b;
>>=Right shift and assigna >>= b;

Lets see each of them in detail.

1. Addition Assignment Operator (+=)

In C++, the addition assignment operator (+=) combines the addition operation with the variable assignment allowing you to increment the value of variable by a specified expression in a concise and efficient way.

Syntax

variable += value;

This above expression is equivalent to the expression:

variable = variable + value;

Example

C++
#include <iostream> using namespace std;  int main() {        int total = 0;      	// Add multiple values and reassign     total += 10;     total += 20;     total += 30;     total += 40;     total += 50;      cout << total;     return 0; } 

Output
150

2. Subtraction Assignment Operator (-=)

The subtraction assignment operator (-=) in C++ enables you to update the value of the variable by subtracting another value from it. This operator is especially useful when you need to perform subtraction and store the result back in the same variable.

Syntax

variable -= value;

This above expression is equivalent to the expression:

variable = variable - value;

Example

C++
#include <iostream> using namespace std;  int main() {     int x = 20;     int y = 5;        // Using the subtraction assignment operator     x -= y;        cout << x;     return 0; } 

Output
15

3. Multiplication Assignment Operator (*=)

In C++, the multiplication assignment operator (*=) is used to update the value of the variable by multiplying it with another value.

Syntax

variable *= value;

This above expression is equivalent to the expression:

variable = variable * value;

Example

C++
#include <iostream> using namespace std;  int main() {     int x = 7;      	// Multiply with 4 and assign it back to x     x *= 4;        cout << x;     return 0; } 

Output
28

4. Division Assignment Operator (/=)

The division assignment operator divides the variable on the left by the value on the right and assigns the result to the variable on the left.

Syntax

variable /= value;

This above expression is equivalent to the expression:

variable = variable / value;

Example

C++
#include <iostream> using namespace std;  int main() {     double x = 30.0;      // Dividing x by 4 and assigning the value to x     x /= 4.0;      cout << x;     return 0; } 

Output
7.5

5. Modulus Assignment Operator (%=)

The modulus assignment operator calculates the remainder when the variable on the left is divided by the value or variable on the right and assigns the result to the variable on the left.

Syntax

variable %= value;

This above expression is equivalent to the expression:

variable = variable % value;

Example

C++
#include <iostream> using namespace std;  int main() {     int x = 15;      // finding the remainder when x is divided by 15 and     // assigning it to a again     x %= 5;      cout << x ;     return 0; } 

Output
0

6. Bitwise AND Assignment Operator (&=)

This operator performs a bitwise AND between the variable on the left and the value on the right and assigns the result to the variable on the left.

Syntax

variable &= value;

This above expression is equivalent to the expression:

variable = variable & value;

Example

C++
#include <iostream> using namespace std;  int main() {     int x = 9;      // using Bitwise AND Assignment Operator     x &= 3;     cout << x;     return 0; } 

Output
1

7. Bitwise OR Assignment Operator (|=)

The bitwise OR assignment operator performs a bitwise OR between the variable on the left and the value or variable on the right and assigns the result to the variable on the left.

Syntax

variable |= value;

This above expression is equivalent to the expression:

variable = variable | value;

Example

C++
#include <iostream> using namespace std;  int main() {     int x = 5; // 0101 in binary      // using Bitwise OR Assignment Operator     x |= 2;        cout << x;     return 0; } 

Output
7

8. Bitwise XOR Assignment Operator (^=)

The bitwise XOR assignment operator performs a bitwise XOR between the variable on the left and the value or variable on the right and assigns the result to the variable on the left.

Syntax

variable ^= value;

This above expression is equivalent to the expression:

variable = variable ^ value;

Example

C++
#include <iostream> using namespace std;  int main() {     int x = 7;      // using Bitwise XOR Assignment Operator     x ^= 3;     cout << x;     return 0; } 

Output
4

9. Left Shift Assignment Operator (<<=)

The left shift assignment operator shifts the bits of the variable on the left to left by the number of positions specified on the right and assigns the result to the variable on the left.

Syntax

variable <<= value;

This above expression is equivalent to the expression:

variable = variable << value;

Example

C++
#include <iostream> using namespace std;  int main() {     int x = 9;      // using Left Shift Assignment Operator     x <<= 4;        cout << x;     return 0; } 

Output
144

10. Right Shift Assignment Operator (>>=)

The right shift assignment operator shifts the bits of the variable on the left to the right by a number of positions specified on the right and assigns the result to the variable on the left.

Syntax

variable >>= value;

This above expression is equivalent to the expression:

variable = variable >> value;

Example

C++
#include <iostream> using namespace std;  int main() {     int x = 19;      // using Right Shift Assignment Operator     x >>= 4;     cout << x;     return 0; } 

Output
1

Also, it is important to note that all of the above operators can be overloaded for custom operations with user-defined data types to perform the operations we want.


Next Article
Assignment Operators in C++

M

maha123
Improve
Article Tags :
  • C++
  • Geeks Premier League
  • Geeks Premier League 2023
Practice Tags :
  • CPP

Similar Reads

    Assignment Operators in Programming
    Assignment operators in programming are symbols used to assign values to variables. They offer shorthand notations for performing arithmetic operations and updating variable values in a single step. These operators are fundamental in most programming languages and help streamline code while improvin
    7 min read
    Solidity - Assignment Operators
    Solidity is a high-level, statically-typed programming language for Ethereum smart contracts. Python, JavaScript, and C++ impact it. Solidity has several variable and value assignment operators. Solidity supports the following types of operators: Simple Assignment Operator.Arithmetic Assignment Oper
    5 min read
    Copy Constructor vs Assignment Operator in C++
    Copy constructor and Assignment operator are similar as they are both used to initialize one object using another object. But, there are some basic differences between them:Copy constructor Assignment operator It is called when a new object is created from an existing object, as a copy of the existi
    2 min read
    C++ Assignment Operator Overloading
    Prerequisite: Operator OverloadingThe assignment operator,"=", is the operator used for Assignment. It copies the right value into the left value. Assignment Operators are predefined to operate only on built-in Data types.Assignment operator overloading is binary operator overloading.Overloading ass
    4 min read
    Self assignment check in assignment operator
    In C++, assignment operator should be overloaded with self assignment check. For example, consider the following class Array and overloaded assignment operator function without self assignment check. C // A sample class class Array { private: int *ptr; int size; public: Array& operator = (const
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences