ā¹ļø Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.2 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://www.includehelp.com/cpp-tutorial/assignments-operators-in-c-cpp.aspx |
| Last Crawled | 2026-04-11 21:38:39 (5 days ago) |
| First Indexed | 2020-06-07 11:42:57 (5 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Assignment Operators in C/C++ |
| Meta Description | C/C++ | Assignment Operators: In this tutorial, we will learn about the various types of assignment operators with their usages, syntax, examples, etc. |
| Meta Canonical | null |
| Boilerpipe Text | Home
Ā»
C++ programming language
C/C++ | Assignment Operators
: In this tutorial, we will learn about the various types of assignment operators with their usages, syntax, examples, etc.
Submitted by
IncludeHelp
, on June 06, 2020
What are Assignment Operators in C/C++?
Assignment operators
are used to assign the value/result of the expression to a variable (constant ā in case of
constant declaration
). While executing an assignment operator based statement, it assigns the value (or the result of the expression) which is written at the right side to the variable which is written on the left side.
Syntax
variable = value;
Type of the assignment operators
C/C++ language provides a
simple assignment operator
that is
"="
, but some of the other
assignment operators
(which are the combination of assignment and other operators) can be used.
The assignment operators are,
SNo.
Operator
Description
Example
1
=
Simple assignment operator
x = 10
2
+=
Add and assignment operator
x += 10
3
-=
Subtract and assignment operator
x -=10
4
*=
Multiply and assignment operator
x *=10
5
/=
Divide and assignment operator
x /=10
6
%=
Modules and assignment operator
x %=10
7
<<=
Left shift and assignment operator
x <<= 10
8
>>=
Right shift and assignment operator
x =>>10
9
&=
Bitwise AND and assignment operator
x &= 10
10
|=
Bitwise OR and assignment operator
x |= 10
11
^|
Bitwise XOR and assignment operator
x ^= 10
Note:
On the right side, a value, expression, or any variable can be used.
1) Simple assignment operator (=)
It is a simple assignment operator which is used to assign the value and the result of the expression to the variable.
Syntax
variable = value;
Example
// C++ program to demonstrate the
// example of = operator
#include <iostream>
using
namespace
std;
int
main
()
{
int
x
=
0
;
x
=
10
;
cout
<<
"value of x = "
<<
x
<<
endl;
return
0
;
}
Output:
value of x = 10
2) Add and assignment operator (+=)
It adds the value or result of the expression to the current value of the variable and assigns the result to the variable.
Syntax
variable += value;
equivalent to:
variable = variable + value;
Example
// C++ program to demonstrate the
// example of += operator
#include <iostream>
using
namespace
std;
int
main
()
{
int
x
=
10
;
cout
<<
"Before the operation, x = "
<<
x
<<
endl;
x
+=
5
;
cout
<<
"After the operation, x = "
<<
x
<<
endl;
return
0
;
}
Output:
Before the operation, x = 10
After the operation, x = 15
3) Subtract and assignment operator (-=)
It subtracts the value or result of the expression to the current value of the variable and assigns the result to the variable.
Syntax
variable -= value;
equivalent to:
variable = variable - value;
Example
// C++ program to demonstrate the
// example of -= operator
#include <iostream>
using
namespace
std;
int
main
()
{
int
x
=
10
;
cout
<<
"Before the operation, x = "
<<
x
<<
endl;
x
-=
5
;
cout
<<
"After the operation, x = "
<<
x
<<
endl;
return
0
;
}
Output:
Before the operation, x = 10
After the operation, x = 5
4) Multiply and assignment operator (*=)
It multiplies the value or result of the expression to the current value of the variable and assigns the result to the variable.
Syntax
variable *= value;
equivalent to:
variable = variable * value;
Example
// C++ program to demonstrate the
// example of *= operator
#include <iostream>
using
namespace
std;
int
main
()
{
int
x
=
10
;
cout
<<
"Before the operation, x = "
<<
x
<<
endl;
x
*=
5
;
cout
<<
"After the operation, x = "
<<
x
<<
endl;
return
0
;
}
Output:
Before the operation, x = 10
After the operation, x = 50
5) Divide and assignment operator (/=)
It divides the value or result of the expression with the current value of the variable and assigns the result (quotient) to the variable.
Syntax
variable /= value;
equivalent to:
variable = variable / value;
Example
// C++ program to demonstrate the
// example of /= operator
#include <iostream>
using
namespace
std;
int
main
()
{
int
x
=
10
;
cout
<<
"Before the operation, x = "
<<
x
<<
endl;
x
/=
5
;
cout
<<
"After the operation, x = "
<<
x
<<
endl;
return
0
;
}
Output:
Before the operation, x = 10
After the operation, x = 2
6) Modules and assignment operator (%=)
It divides the value or result of the expression with the current value of the variable and assigns the result (remainder) to the variable.
Syntax
variable %= value;
equivalent to:
variable = variable % value;
Example
// C++ program to demonstrate the
// example of %= operator
#include <iostream>
using
namespace
std;
int
main
()
{
int
x
=
10
;
cout
<<
"Before the operation, x = "
<<
x
<<
endl;
x
%=
5
;
cout
<<
"After the operation, x = "
<<
x
<<
endl;
return
0
;
}
Output:
Before the operation, x = 10
After the operation, x = 0
7) Left shift and assignment operator (<<=)
It shifts the value of the variable by given number of bits (value) to the left and assigns the result to the variable.
Syntax
variable <<= value;
equivalent to:
variable = variable << value;
Example
// C++ program to demonstrate the
// example of <<= operator
#include <iostream>
using
namespace
std;
int
main
()
{
int
x
=
0x0A
;
cout
<<
"Before the operation, x = "
<<
x
<<
endl;
x
<<=
0x02
;
cout
<<
"After the operation, x = "
<<
x
<<
endl;
return
0
;
}
Output:
Before the operation, x = 10
After the operation, x = 40
8) Right shift and assignment operator (>>=)
It shifts the value of the variable by the given number of bits (value) to the right and assigns the result to the variable.
Syntax
variable >>= value;
equivalent to:
variable = variable >> value;
Example
// C++ program to demonstrate the
// example of >>= operator
#include <iostream>
using
namespace
std;
int
main
()
{
int
x
=
0x0A
;
cout
<<
"Before the operation, x = "
<<
x
<<
endl;
x
>>=
0x02
;
cout
<<
"After the operation, x = "
<<
x
<<
endl;
return
0
;
}
Output:
Before the operation, x = 10
After the operation, x = 2
9) Bitwise AND and assignment operator (&=)
It performs the Bitwise AND (&) operation on the existing value of the variable with the given value and assigns the result to the variable.
Syntax
variable &= value;
equivalent to:
variable = variable & value;
Example
// C++ program to demonstrate the
// example of &= operator
#include <iostream>
using
namespace
std;
int
main
()
{
int
x
=
0x0A
;
cout
<<
"Before the operation, x = "
<<
x
<<
endl;
x
&=
0x02
;
cout
<<
"After the operation, x = "
<<
x
<<
endl;
return
0
;
}
Output:
Before the operation, x = 10
After the operation, x = 2
10) Bitwise OR and assignment operator (|=)
It performs the Bitwise OR (|) operation on the existing value of the variable with the given value and assigns the result to the variable.
Syntax
variable |= value;
equivalent to:
variable = variable | value;
Example
// C++ program to demonstrate the
// example of |= operator
#include <iostream>
using
namespace
std;
int
main
()
{
int
x
=
0x0A
;
cout
<<
"Before the operation, x = "
<<
x
<<
endl;
x
|=
0x02
;
cout
<<
"After the operation, x = "
<<
x
<<
endl;
return
0
;
}
Output:
Before the operation, x = 10
After the operation, x = 10
11) Bitwise XOR and assignment operator (^=)
It performs the Bitwise XOR (^) operation on the existing value of the variable with the given value and assigns the result to the variable.
Syntax
variable ^= value;
equivalent to:
variable = variable ^ value;
Example
// C++ program to demonstrate the
// example of ^= operator
#include <iostream>
using
namespace
std;
int
main
()
{
int
x
=
0x0A
;
cout
<<
"Before the operation, x = "
<<
x
<<
endl;
x
^=
0x02
;
cout
<<
"After the operation, x = "
<<
x
<<
endl;
return
0
;
}
Output:
Before the operation, x = 10
After the operation, x = 8
C++ program to demonstrate the example of various assignment operators
// C++ program to demonstrate the example
// of various assignment operators
#include <iostream>
using
namespace
std;
int
main
()
{
int
x
=
0
;
// = operator
x
=
20
;
cout
<<
"x = "
<<
x
<<
endl;
// += operator
x
+=
5
;
cout
<<
"x = "
<<
x
<<
endl;
// -= operator
x
-=
5
;
cout
<<
"x = "
<<
x
<<
endl;
// *= operator
x
*=
5
;
cout
<<
"x = "
<<
x
<<
endl;
// /= operator
x
/=
3
;
cout
<<
"x = "
<<
x
<<
endl;
// %= operator
x
%=
5
;
cout
<<
"x = "
<<
x
<<
endl;
// <<= operator
x
<<=
5
;
cout
<<
"x = "
<<
x
<<
endl;
// >>= operator
x
>>=
5
;
cout
<<
"x = "
<<
x
<<
endl;
// &= operator
x
&=
5
;
cout
<<
"x = "
<<
x
<<
endl;
// |= operator
x
|=
5
;
cout
<<
"x = "
<<
x
<<
endl;
// ^= operator
x
^=
10
;
cout
<<
"x = "
<<
x
<<
endl;
return
0
;
}
Output:
x = 20
x = 25
x = 20
x = 100
x = 33
x = 3
x = 96
x = 3
x = 1
x = 5
x = 15
Advertisement
Advertisement
Learn & Test Your Skills |
| Markdown | null |
| Readable Markdown | [Home](https://www.includehelp.com/) Ā» [C++ programming language](https://www.includehelp.com/cpp-tutorial)
**C/C++ \| Assignment Operators**: In this tutorial, we will learn about the various types of assignment operators with their usages, syntax, examples, etc.
Submitted by **IncludeHelp**, on June 06, 2020
## What are Assignment Operators in C/C++?
**Assignment operators** are used to assign the value/result of the expression to a variable (constant ā in case of [constant declaration](https://www.includehelp.com/cpp-tutorial/difference-between-const-and-define.aspx)). While executing an assignment operator based statement, it assigns the value (or the result of the expression) which is written at the right side to the variable which is written on the left side.
## Syntax
```
variable = value;
```
## Type of the assignment operators
C/C++ language provides a **simple assignment operator** that is **"="**, but some of the other **assignment operators** (which are the combination of assignment and other operators) can be used.
The assignment operators are,
| | | | |
|---|---|---|---|
| SNo. | Operator | Description | Example |
| 1 | **\=** | Simple assignment operator | x = 10 |
| 2 | **\+=** | Add and assignment operator | x += 10 |
| 3 | **\-=** | Subtract and assignment operator | x -=10 |
| 4 | **\*=** | Multiply and assignment operator | x \*=10 |
| 5 | /= | Divide and assignment operator | x /=10 |
| 6 | **%=** | Modules and assignment operator | x %=10 |
| 7 | **\<\<=** | Left shift and assignment operator | x \<\<= 10 |
| 8 | **\>\>=** | Right shift and assignment operator | x =\>\>10 |
| 9 | **&=** | Bitwise AND and assignment operator | x &= 10 |
| 10 | **\|=** | Bitwise OR and assignment operator | x \|= 10 |
| 11 | **^\|** | Bitwise XOR and assignment operator | x ^= 10 |
**Note:** On the right side, a value, expression, or any variable can be used.
## 1\) Simple assignment operator (=)
It is a simple assignment operator which is used to assign the value and the result of the expression to the variable.
### Syntax
```
variable = value;
```
### Example
```
// C++ program to demonstrate the
// example of = operator
#include <iostream>
using namespace std;
int main()
{
int x = 0;
x = 10;
cout << "value of x = " << x << endl;
return 0;
}
```
**Output:**
```
value of x = 10
```
## 2\) Add and assignment operator (+=)
It adds the value or result of the expression to the current value of the variable and assigns the result to the variable.
### Syntax
```
variable += value;
equivalent to: variable = variable + value;
```
### Example
```
// C++ program to demonstrate the
// example of += operator
#include <iostream>
using namespace std;
int main()
{
int x = 10;
cout << "Before the operation, x = " << x << endl;
x += 5;
cout << "After the operation, x = " << x << endl;
return 0;
}
```
**Output:**
```
Before the operation, x = 10
After the operation, x = 15
```
## 3\) Subtract and assignment operator (-=)
It subtracts the value or result of the expression to the current value of the variable and assigns the result to the variable.
### Syntax
```
variable -= value;
equivalent to: variable = variable - value;
```
### Example
```
// C++ program to demonstrate the
// example of -= operator
#include <iostream>
using namespace std;
int main()
{
int x = 10;
cout << "Before the operation, x = " << x << endl;
x -= 5;
cout << "After the operation, x = " << x << endl;
return 0;
}
```
**Output:**
```
Before the operation, x = 10
After the operation, x = 5
```
## 4\) Multiply and assignment operator (\*=)
It multiplies the value or result of the expression to the current value of the variable and assigns the result to the variable.
### Syntax
```
variable *= value;
equivalent to: variable = variable * value;
```
### Example
```
// C++ program to demonstrate the
// example of *= operator
#include <iostream>
using namespace std;
int main()
{
int x = 10;
cout << "Before the operation, x = " << x << endl;
x *= 5;
cout << "After the operation, x = " << x << endl;
return 0;
}
```
**Output:**
```
Before the operation, x = 10
After the operation, x = 50
```
## 5\) Divide and assignment operator (/=)
It divides the value or result of the expression with the current value of the variable and assigns the result (quotient) to the variable.
### Syntax
```
variable /= value;
equivalent to: variable = variable / value;
```
### Example
```
// C++ program to demonstrate the
// example of /= operator
#include <iostream>
using namespace std;
int main()
{
int x = 10;
cout << "Before the operation, x = " << x << endl;
x /= 5;
cout << "After the operation, x = " << x << endl;
return 0;
}
```
**Output:**
```
Before the operation, x = 10
After the operation, x = 2
```
## 6\) Modules and assignment operator (%=)
It divides the value or result of the expression with the current value of the variable and assigns the result (remainder) to the variable.
### Syntax
```
variable %= value;
equivalent to: variable = variable % value;
```
### Example
```
// C++ program to demonstrate the
// example of %= operator
#include <iostream>
using namespace std;
int main()
{
int x = 10;
cout << "Before the operation, x = " << x << endl;
x %= 5;
cout << "After the operation, x = " << x << endl;
return 0;
}
```
**Output:**
```
Before the operation, x = 10
After the operation, x = 0
```
## 7\) Left shift and assignment operator (\<\<=)
It shifts the value of the variable by given number of bits (value) to the left and assigns the result to the variable.
### Syntax
```
variable <<= value;
equivalent to: variable = variable << value;
```
### Example
```
// C++ program to demonstrate the
// example of <<= operator
#include <iostream>
using namespace std;
int main()
{
int x = 0x0A;
cout << "Before the operation, x = " << x << endl;
x <<= 0x02;
cout << "After the operation, x = " << x << endl;
return 0;
}
```
**Output:**
```
Before the operation, x = 10
After the operation, x = 40
```
## 8\) Right shift and assignment operator (\>\>=)
It shifts the value of the variable by the given number of bits (value) to the right and assigns the result to the variable.
### Syntax
```
variable >>= value;
equivalent to: variable = variable >> value;
```
### Example
```
// C++ program to demonstrate the
// example of >>= operator
#include <iostream>
using namespace std;
int main()
{
int x = 0x0A;
cout << "Before the operation, x = " << x << endl;
x >>= 0x02;
cout << "After the operation, x = " << x << endl;
return 0;
}
```
**Output:**
```
Before the operation, x = 10
After the operation, x = 2
```
## 9\) Bitwise AND and assignment operator (&=)
It performs the Bitwise AND (&) operation on the existing value of the variable with the given value and assigns the result to the variable.
### Syntax
```
variable &= value;
equivalent to: variable = variable & value;
```
### Example
```
// C++ program to demonstrate the
// example of &= operator
#include <iostream>
using namespace std;
int main()
{
int x = 0x0A;
cout << "Before the operation, x = " << x << endl;
x &= 0x02;
cout << "After the operation, x = " << x << endl;
return 0;
}
```
**Output:**
```
Before the operation, x = 10
After the operation, x = 2
```
## 10\) Bitwise OR and assignment operator (\|=)
It performs the Bitwise OR (\|) operation on the existing value of the variable with the given value and assigns the result to the variable.
### Syntax
```
variable |= value;
equivalent to: variable = variable | value;
```
### Example
```
// C++ program to demonstrate the
// example of |= operator
#include <iostream>
using namespace std;
int main()
{
int x = 0x0A;
cout << "Before the operation, x = " << x << endl;
x |= 0x02;
cout << "After the operation, x = " << x << endl;
return 0;
}
```
**Output:**
```
Before the operation, x = 10
After the operation, x = 10
```
## 11\) Bitwise XOR and assignment operator (^=)
It performs the Bitwise XOR (^) operation on the existing value of the variable with the given value and assigns the result to the variable.
### Syntax
```
variable ^= value;
equivalent to: variable = variable ^ value;
```
### Example
```
// C++ program to demonstrate the
// example of ^= operator
#include <iostream>
using namespace std;
int main()
{
int x = 0x0A;
cout << "Before the operation, x = " << x << endl;
x ^= 0x02;
cout << "After the operation, x = " << x << endl;
return 0;
}
```
**Output:**
```
Before the operation, x = 10
After the operation, x = 8
```
**C++ program to demonstrate the example of various assignment operators**
```
// C++ program to demonstrate the example
// of various assignment operators
#include <iostream>
using namespace std;
int main()
{
int x = 0;
// = operator
x = 20;
cout << "x = " << x << endl;
// += operator
x += 5;
cout << "x = " << x << endl;
// -= operator
x -= 5;
cout << "x = " << x << endl;
// *= operator
x *= 5;
cout << "x = " << x << endl;
// /= operator
x /= 3;
cout << "x = " << x << endl;
// %= operator
x %= 5;
cout << "x = " << x << endl;
// <<= operator
x <<= 5;
cout << "x = " << x << endl;
// >>= operator
x >>= 5;
cout << "x = " << x << endl;
// &= operator
x &= 5;
cout << "x = " << x << endl;
// |= operator
x |= 5;
cout << "x = " << x << endl;
// ^= operator
x ^= 10;
cout << "x = " << x << endl;
return 0;
}
```
**Output:**
```
x = 20
x = 25
x = 20
x = 100
x = 33
x = 3
x = 96
x = 3
x = 1
x = 5
x = 15
```
Advertisement
Advertisement
**Learn & Test Your Skills** |
| Shard | 106 (laksa) |
| Root Hash | 1184066710662109106 |
| Unparsed URL | com,includehelp!www,/cpp-tutorial/assignments-operators-in-c-cpp.aspx s443 |