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.
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
- Arithmetic Operators
- C++ Shorthands
- Unary Operator
- Increment and Decrement Operators
- Relation Operator
- Logical Operators
Types of Assignment Operators
In C++, there are 2 categories of Assignment Operators
- Simple Assignment Operator
- 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
a = a + 10 ;
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?
- ! =
- = =
- =
- None of the above
Answer. Option C
Q2. Assignment Operator is an example of ______ operator.
- Relational
- Binary
- Unary
- Conditional
Answer. Option B
Q3. Which of the following is an invalid assignment operator?
- a %= 10 ;
- a /= 5;
- a .= 17 ;
- None of the Above
Answer. Option C
Leave a Reply