Operator and Expressions: Operators

Assignment Operators

Just like Arithmetic Operators, C++ has Assignment Operators. In order to give value to the variable in the program, an operator is needed. This function is provided by the Assignment Operators. Assignment Operators in programming languages are pre-defined operators which are used to assign values to the variables. Let us look at these Assignment operators in more detail.

assignment operators

Definition

  • Assignment Operators are used to assigning the results of an expression or constant to a variable.
  • In order to assign values to a variable, we use the Assignment Operator in the programming language.

Assignment Operators

Assignment Operator is a type of Binary Operator wherein it needs two operands to function. The left side operand is the variable and the right side of the operand is the value to be assigned.

The symbol which is used by the Assignment Operator is ‘ = ’.

Syntax

                    

variable_name = constant / expression / value ;

The assignment operator can be used within any valid expression. An expression can be a combination of variables, constants, and operators arranged as per the syntax and precedence levels of the programming language.

During the use of the assignment operator, always the right-hand side of the operator is computed first, and then it is assigned to the variable defined on the left-hand side of the statement.

Example

                    

int x = 20 ;                  // value 20 is assigned to variable ‘x’

int a = (5)^2 ;            // expression on the right side is first computed and then int value 25 is assigned to variable ‘a’

char c = ‘Z ’ ;              // character Z is assigned to variable ‘c’

Note: The source (right-hand side) and destination (left-hand side) should always be of the same data type.

Browse all the Topics Under Operator and Expressions: Operators

Types of Assignment Operators

In C++, there are 2 categories of Assignment Operators

  1. Simple Assignment Operator
  2. Compound Assignment Operator

Simple Assignment Operator (=) :

This is the usual assignment operator. Simple-assignment operator assigns its right operand to its left operand. In easy terms, this operator is used to assign the value on the right to the variable on the left.

Example

                    

int speed = 24 ;

float ratio = 0.34 ;

Compound Assignment Operator:

These type of Operators are also known as Short-hand assignment operators.

Its syntax is,

                    

variable OP = expression / value ;

where OP is a binary operator and is known as short-hand assignment operator.

Example

                    

x += y ;             // equivalent to x = x + y

x -= y ;             // equivalent to x = x - y

a %= b ;           // equivalent to a = a % b

a *= b ;            // equivalent to a = a * b

a /= b ;            // equivalent to a = a / b

Note

While first initializing the variable, we cannot use the Short-hand operators. This will give a declaration error at the compile time of the program. Let’s understand this by looking at an example –

                    

int main()

{

    int a += 10;

    cout<<"Value of a : " << a << endl;

    return 0;

}

Output

Compile-time Error. Value of variable ‘a’ not defined.

In the above program, we can see that in the initialization statement, we did not assign the value of 10 to the variable ‘a’ but instead we used the shorthand assignment operator which means

But the value of ‘a’ has not been defined. The compiler does not know the value stored in the location of variable ‘a’. Hence a compile-time error occurs.

FAQs on Assignment Operators

Q1. Equality Operator is denoted by?

  1. ! =
  2. = =
  3. =
  4. None of the above

Answer. Option C

Q2. Assignment Operator is an example of ______ operator.

  1. Relational
  2. Binary
  3. Unary
  4. Conditional

Answer. Option B

Q3. Which of the following is an invalid assignment operator?

  1. a %= 10 ;
  2. a /= 5;
  3. a .= 17 ;
  4. None of the Above

Answer. Option C

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.