Index
Operators
An operator is a symbol that tells the compiler to perform a specific operation. C language has the following types of operators −
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
1. Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc on numerical values (constants and variables).
Operator | Name | Description |
+ | (Additional Operator) | Adds two operands. |
– | (Subtraction Operator) | Subtracts second operand from the first. |
* | (Multiplication Operator) | Multiplies two operands. |
/ | (Division Operator) | Divides numerator by denominator. |
% | (Modulus Operator) | Remainder after division (modulo division) |
++ | (Increment Operator) | Increases the integer value by one. |
– – | (Decrement Operator) | Decreases the integer value by one. |
Example :
// Example of arithmetic operators
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
c = a++;
printf("Incremented val of a");
c = a--;
printf("Decremented val of a");
return 0;
}
// program to check even and odd
#include <stdio.h>
int main()
{
int a;
printf("enter a number \n");
scanf("%d", &a);
if (a % 2 == 0)
{
printf("%d is even number\n", a);
}
else
{
printf("%d is odd number \n", a);
}
return 0;
}
// OUTPUT:
enter a number: 5
5 is odd number
2.Relational operator:
Relational operators are used to perform various relational operations in C programming.
Definition: Relational operators are binary meaning they require two operands. Relational operators have left to right associativity.
Symbol Operator name Example
== | Equal to | 4 == 2 is evaluated to 0 |
> | Greater than | 4 > 2 is evaluated to 1 |
< | Less than | 4 < 2 is evaluated to 0 |
!= | Not equal to | 4!= 2 is evaluated to 1 |
>= | Greater than or equal to | 4 >= 2 is evaluated to 1 |
<= | Less than or equal to | 4<= 2 is evaluated to 0 |
Basically this is used for comparison between two operands and it is very easy to understand.
Let’s start with example;
#include <stdio.h>
int main()
{
// realtional operator
int a = 10;
int b = 5;
printf("a==b=%d \n", a == b);
printf("a!=b=%d\n", a != b);
printf("a>b=%d \n", a > b);
printf("a<b=%d \n", a < b);
return 0;
}
OUTPUT:
a==b=0
a!=b=1
a>b=1
a<b=0
3.Logical operator:
They are used to combine two or more conditions or to complement the evaluation of the original condition under consideration. Each type of logical operator are discussed below:
3.1 Logical AND operator: The ‘&&’ operator returns true when both the conditions are satisfied. Otherwise, it returns false. For example, a && b returns true when both a and b are true (i.e. non-zero).
3.2 Logical OR operator: The ‘||’ operator returns true even if one (or both) of the conditions are satisfied. Otherwise, it returns false. For example, a || b returns true if one of a or b, or both are true (i.e. non-zero). Of course, it returns true when both a and b are true.
3.3 Logical NOT operator: The ‘!’ operator returns true if the condition is not satisfied. Otherwise, it returns false. For example, !a returns true if a is false, i.e. when a=0.
Lets understand with example:
Example:write a C program by using all logical operations.
#include <stdio.h>
int main()
{
int a = 10, b = 10, c = 20;
if((a == b) && (c > b)){
printf("(a == b) && (c > b) then true \n");
}
if( (a == b) && (c < b)){
printf("(a == b) && (c < b) then false\n");
}
if( (a == b) || (c < b)){
printf("(a == b) || (c < b) then false\n");
}
if( (a != b) || (c < b)){
printf("(a != b) || (c < b) then false\n");
}
if( !(a != b)){
printf("!(a != b) is then true \n");
}
if(!(a == b)){
printf("!(a == b) is then false\n");
}
return 0;
}
4. Bitwise operator:
Bitwise operators work only at bit operation.when we perform bitwise operation then it is also known as bit level programming,this operator calculates only two digits (0 & 1).
In C different type of bitwise operator are used,they are:-
Operator name | Symbol |
Bitwise AND | & |
Bitwise OR | | |
Bitwise XOR | ^ |
Binary one’s complement | ~ |
Binary left shift operator | >> |
Binary right shift operator | << |
Now look at on truth table so that you can understand smoothly:
A | B | A&B | A|B | A^B |
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
Now simply describe one by one BITWISE OPERATOR:
- Bitwise AND Operator :In bitwise AND operator, two operands are written on both sides of the (&) operator. If the corresponding bits of both the operands are 1, then the output of the bitwise AND operation is 1; otherwise, the output would be 0.
Example:write a C program by using bitwise AND operator.
#include <stdio.h>
int main()
{
int a = 5;
int b = 6;
printf("value of AND operator of a&b is = %d", a & b);
return 0;
}
Output:
value of AND operator of a&b is = 4
- Bitwise OR Operator:In bitwise OR operator, two operands are written on both sides of the (|) operator. If the corresponding bits of any of the operands are 1, then the output of the bitwise OR operation is 1; otherwise, the output would be 0.
Example:write a C program by using bitwise OR operator.
#include <stdio.h>
int main()
{
int a = 7;
int b = 5;
printf("value of OR operator of a|b is = %d", a | b);
return 0;
}
Output:
value of OR operator of a|b is = 7
5. Assignment operator:
The assignment operator is used to assign the value in a variable. The most used assignment variable is =.
Assignment operator name | Example | Used |
= | a=6 | a=6 |
+= | a+=6 | a=a+6=12 |
-= | a-=6 | a=a-6 =6 |
-= | a*=6 | a=a*6 =36 |
/= | a/=6 | a=a/6=6 |
%= | a%=6 | a=a%6=0 |
Example:write a C program by using the Assignment operator.
// Example of assignment operators
#include <stdio.h>
int main()
{
int a = 6, b;
b = a; // b is 6
printf("b = %d\n", b);
b += a; // b is 12
printf("b = %d\n", b);
b -= a; // b is 6
printf("b = %d\n", b);
b *= a; // b is 36
printf("b = %d\n", b);
b/= a; // b is 6
printf("b = %d\n", b);
b %= a; // b = 0
printf("b = %d\n", b);
return 0;
}
Output:
b = 6
b = 12
b = 6
b = 36
b = 6
b = 0