Operator is basically use to perform the arithmetical, logical and relational operations.
C programming language have a number of operators:
(A) - Arithmetic Operator : - An arithmetic operator is basically use to perform the mathematical operations like addition(+), subtraction(-), multiplication(*), division(/) and modulo(%).
addition(+) operator is use for perform the addition of operands.
subtraction(-) operator is use for perform the subtraction of operands.
multiplication(*) operator is use for perform the multiplication of operands.
division(/) operator is use for perform the division of operands and give us quotient of them.
modulo(%) operator is use for calculate the remainder of operands on division as a result and it discard the quotient.
So the main difference between the (/) and (%) is that divison operator is used to calculate the quotient while the modulo operator is used to calculate the remainder over division.
Suppose you have two variables x = 6, y = 4
In normal calculation you will get the result of (6/4) = 1.5. but you will get 1 instead of getting 1.5 as result in programming. Because both variables are of the integer types so that it will neglect the decimal part.
If you want to apply the modulo operator on the same variable (6 % 4) then you will get 2 as result . Because % operator calculates the remainder only.
Below is the small example of Arithmetic operator that will cover all operations inside the Arithemtic Operator.
#include
int main(void) {
// your code goes here
int x = 6, y = 4;
printf("%d %d %d %d %d", (x+y), (x-y), (x*y), (x/y), (x%y));
return 0;
}
(B) - Logical Operator :- Logical Operator is basically used for desicion making that will return either 0(false) or 1(true) . Logocal operator mainly have three operator that is Logical AND(&&), Logical OR(||) and LogicalNot(!).
LogicalAND return true only if all operands are true.
LogicalOR return true if any one of operand is true.
LogicalNot return true if operand is false and it return false when operand is true.
Below is the small example of Logical operator that will cover all operations inside the Logical Operator.
#include
int main(void) {
// your code goes here
int x = 6, y = 4;
printf("%d %d %d ", (x&&y), (x||y), (x!=y));
return 0;
}
(C) - Relational Operator :- Relational Operator is basically use for checking relation between two operands. It relation is true it return true(1) and if relation is false it return false(0). There are some relational operator listed below:
Less than(<)
Greater than(>)
Less than or equal to(<=)
Greater than or equal to(>=)
Equal to(==)
Not equal to(!=)
Below is the small example of Relational operator that will cover all operations inside the Relational Operator.
#include
int main(void) {
// your code goes here
int x = 6, y = 4;
printf("%d %d %d %d %d %d", (xy), (x<=y), (x>=y), (x==y), (x!=y));
return 0;
}
These above three are most useful operator in C. Except to this C have other operators as well like:
Increment(++) and Decrement Opeartor(--) -: These both operator are basically use for changing the value of the operand by 1. Increment operator change the value by increaing 1 while Decrement operator change the value by decreasing by 1.
Assignment Operator -: This operator is used for assigning the value to the variable. These are also have various operators like (=, +=, -=, *=, /=, %=)
Bitwise Operator -: This operator is basically used for performing bitwise operations like:
Bitwise AND(&)
Bitwise OR(|)
Bitwise exclusive OR(^)
Bitwise complement(~)
Shift left(<<)
Shift right(>>)
I hope this answer will be useful.