An operator is a symbol that tells the compiler to perform specific mathematical or logical functions.
Let’s understand the arithmetic operators with the help of examples.
#include<stdio.h>
void main(){
int a = 20;
int b = 10;
printf("%i\n", a+b); //Adds two operands -- 30
printf("%i\n", a-b); //Substracts second operand from the first -- 10
printf("%i\n", a*b); //Multiplies both operands -- 200
printf("%i\n", a/b); //Divide numerator by De-nomerator -- 2
printf("%i\n", a%b); //Modulus Operator and remainder of after an integer division -- 0
printf("%i\n", a++); //Increment operator increases the integer value by one after the current operation -- 20
printf("%i\n", a--); //Decrement operator decreases the integer value by one after the current operation -- 10
printf("%i\n", ++a); //Increment operator increases the integer value by one -- 21
printf("%i\n", --a); //Decrement operator decreases the integer value by one -- 20
}
A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0.
Lets understand the relational operators
#include<stdio.h>
void main(){
int a = 10;
int b = 7;
printf("%d\n", a==b); //equal to -- 0
printf("%d\n", a<b); //less than -- 0
printf("%d\n", a>b); //greater than -- 1
printf("%d\n", a<=b); //less than or equal to -- 0
printf("%d\n", a>=b); //greater than or equal to -- 1
printf("%d\n", a!=b); //not equal to -- 1
}
An expression containing a logical operator returns either 0 or 1 depending upon whether the expression results true or false. Logical operators are commonly used in decision making in C.
Let’s understand the logical operators with the examples:
#include<stdio.h>
void main(){
int a=5;
int b = 10;
int c =15;
int result;
result = (a == b) && (c >b); //Logical AND. True only if all operands are true.
printf("%d \n", result); // -- 0
result = (a == b) || (c < b); //Logical OR. True only if all operands is true.
printf("%d \n", result); // -- 0
result = !(a != b); //Logical OR. True only if either one operand is true.
printf("%d \n", result); // -- 0
}
During computation, mathematical operations like: addition, subtraction, multiplication, division, etc are converted to bit-level which makes processing faster and saves power.
Lets understand with an example:
#include<stdio.h>
void main(){
int x =10;
int y = 9;
printf("%d\n", x&y); //bitwise AND --8
printf("%d\n", x||y); //bitwise OR --1
printf("%d\n", x^y); //bitwise exclusive OR --3
printf("%d\n", x>>y); //bitwise right --0
printf("%d\n", x<<y); //bitwise left --5120
printf("%d\n", x-y); //bitwise complement --1
}