GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||
GeezerVison is showing www.geeksforgeeks.org — Assignment Operators in C++ - GeeksforGeeks Assignment Operators in C++Last Updated : 31 Mar, 2026 Assignment operator is used to assign a value to a variable. It is denoted by equal sign ( = ) and stores the value of the right-hand expression into the memory location of the left-hand variable. For Example
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
The right-hand side value will be assigned to the variable on the left-hand side. The variable and the value should be compatible data types. C++ allows implicit type conversions during assignment, although this may lead to data loss in some cases. The value can be a literal or another variable of the same data type. Compound Assignment OperatorsIn 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++:
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
This above expression is equivalent to the expression:
Example
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
This above expression is equivalent to the expression:
Example
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
This above expression is equivalent to the expression:
Example
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
This above expression is equivalent to the expression:
Example
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
This above expression is equivalent to the expression:
Example
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
This above expression is equivalent to the expression:
Example
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
This above expression is equivalent to the expression:
Example
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
This above expression is equivalent to the expression:
Example
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
This above expression is equivalent to the expression:
Example
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
This above expression is equivalent to the expression:
Example
Output 1 Many of these operators can be overloaded for user-defined types. However, operator overloading does not apply to built-in types, and certain operators have limitations or cannot be overloaded at all. Comment ExploreStandard Template Library(STL) Practice & Problems | |||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |