Prev Next
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.
1 2 3 4 5 6 7 8 9 10 11 |
# include <stdio.h> int main() { int Total=0,i; for(i=0;i<10;i++) { Total+=i; // This is same as Total = Toatal+i } printf("Total = %d", Total); } |
Output:
Total = 45
|
Continue on types of C operators:
Click on each operator name below for detailed description and example programs.
Types of Operators
|
Description
|
Arithmetic_operators | These are used to perform mathematical calculations like addition, subtraction, multiplication, division and modulus |
Assignment_operators | These are used to assign the values for the variables in C programs. |
These operators are used to compare the value of two variables.
|
|
These operators are used to perform logical operations on the given two variables.
|
|
These operators are used to perform bit operations on given two variables.
|
|
Conditional operators return one value if condition is true and returns another value is condition is false.
|
|
These operators are used to either increase or decrease the value of the variable by one.
|
|
&, *, sizeof( ) and ternary operators.
|