ASSIGNMENT OPERATORS IN C:
- In C programs, values for the variables are assigned using assignment operators.
- For example, if the value “10” is to be assigned for the variable “sum”, it can be assigned as “sum = 10;”
- There are 2 categories of assignment operators in C language. They are,
1. Simple assignment operator ( Example: = )
2. Compound assignment operators ( Example: +=, -=, *=, /=, %=, &=, ^= )
Operators
|
Example/Description
|
=
|
sum = 10;
10 is assigned to variable sum |
+=
|
sum += 10;
This is same as sum = sum + 10
|
-=
|
sum -= 10;
This is same as sum = sum – 10
|
*=
|
sum *= 10;
This is same as sum = sum * 10
|
/=
|
sum /= 10;
This is same as sum = sum / 10
|
%=
|
sum %= 10;
This is same as sum = sum % 10
|
&=
|
sum&=10;
This is same as sum = sum & 10
|
^=
|
sum ^= 10;
This is same as sum = sum ^ 10
|
EXAMPLE PROGRAM FOR C ASSIGNMENT OPERATORS:
- In this program, values from 0 – 9 are summed up and total “45” is displayed as output.
- Assignment operators such as “=” and “+=” are used in this program to assign the values and to sum up the values.
OUTPUT:
Total = 45
|
No comments:
Post a Comment