Assignment Operators in C/C++

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

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



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.