Assignment operator:C Programming Language - Part 2.1
Assignment operator
Assignment operator assigns a value to the variable. The assignment is used as follows
Identifier = expression
Here the identifier is the variable, where we will put the value. And expression is any value. Such as
Pi = 3.1416;
The value on the right can be any expression. Such as
Pi = 22/7;
Equal to assignment operator we have used before. In addition to this, there are many more assignment operators. Such as
1) = (Equal to)
2) + = (Plas equal to)
3) - = (Mainus equal to)
4) * = (Product equal to)
5) / = (Division equal to)
6)% = (Mode equal to) etc.
However, the most used assignment operator is = (Equal to). It is written from below.
Identifier = expression
Here the identifier is usually used to mean variables. And expression means any value such as constant or any other variable.
Below are some examples of assignment operators:
X = 5;
Y = 10;
Pi = 3.1416
Z = x + y + pi
Here the first example is assigned between 5, x. That is, the value of x is now 5 The second example is assigned between 10, y. That is, the value of y is now 10. In the third example, 3.1416 has been assigned to pi. That is, the value of pi is now 3.1416. The fourth example (x + y + pi) is assigned between z. That is, the value of z is now (x + y + pi).
We have already used the assignment operator.
I saw a previous program, where we found out the volume of rectangular land.
1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
Int main ()
{
Int volume;
Int length = 5;
Int width = 8;
Volume = length * width;
Printf ("% d", volume);
Return 0;
}
Where we put a value [5] on a variable called length. And in the width vector 8
+ = (Plas equal to) Assignment operator:
It is written as follows
Expression1 + = Expression2;
That is equal to (Expression1 = Expression1 + Expression2).
Explanation: Thinks x = 3, y = 5
If it is written: x + = y, the value of x is x = x + y is equal to x = 8
1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
Int main ()
{
Int x = 3;
Int y = 5;
X + = y;
Printf ("% d", x);
Return 0;
}
If we run the above program, we will get the output: 8
See the following program
1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
Int main ()
{
Int x = 3;
Int y = 5;
X = x + y;
Printf ("% d", x);
Return 0;
}
If we run this program then we will get the output: 8
There is no difference in the output. To write fewer codes, the operator is often used.
- = (Mainus equal to) Assignment operator
The following is written as follows:
Exprission1 - = Expression2;
That is equal to (Expression1 = Expression1 - Expression2).
Explanation: Thinks x = 8, y = 5
If it is written: x- = y, then the value of x is x = x-y, x = 3.
1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
Int main ()
{
Int x = 8;
Int y = 5;
X - = y;
Printf ("% d", x);
Return 0;
}
If we run the above program, we will get the output:
See the following program
1
2
3
4
5
6
7
8
9
10
#include
Int main ()
{
Int x = 8;
Int y = 5;
X = x-y;
Printf ("% d", x);
Return 0;
}
If we run the program then we will get the output:
There is no difference in the output with the above program. + = About time = = is used to write the same code as the operator.
In the same way * = means:
Exprission1 * = Expression2;
That is equal to (Expression1 = Expression1 * Expression2).
/ = Means:
Expression1 / = Expression2;
That is equal to (Expression1 = Expression1 / Expression2).
% = Means:
Expression1% = Expression2;
That's equal to (Expression1 = Expression1% Expression2).
No comments