Assignment Operator :
An Assignment operator is used to form an assignment expression, which assigns the value to an identifier. The most commonly used assignment operator is = . Assignment expressions that make use of this operator are written in the form<
Other forms of assignment operators exist that are obtaining various operators such as +,-,*,/,% etc with the = sign.
C – ASSIGNMENT OPERATORS EXAMPLE PROGRAM :
|Value of a = 123|
Value of b = 125
In this tutorial we will learn about assignment operators in C programming language.
We use the assignment operators to assign the result of an expression to a variable.
In the following example we are assigning integer value 10 to a variable score of data type integer.
Shorthand Assignment Operators
Lets say we have an integer variable and we have initially assigned 10 to it. Then lets say we increase the value by 5 and assign the new value to it.
Another way to write the code is by using the shorthand assignment as shown below.
Following is a list of shorthand assignment operators.
|Simple assignment||Shorthand assignment|
|x = x + 1||x += 1|
|x = x - 1||x -= 1|
|x = x * (n + 1)||x *= (n + 1)|
|x = x / (n + 1)||x /= (n + 1)|
|x = x % y||x %= y|
In the following example we are taking a value x from user then we are taking a new value y from user. Then we are adding y to x and assigning the result to x. Finally we are printing the new value stored in variable x.
Another way of adding y to x and re-assigning the new value to x is by using the shorthand assignment operator .