Operators in C Language
What are operators in C ?
Operators are symbols that help us to perform mathematical and logical operations in the program (or) code. These are used to perform the action between operands that is between two (or) more variables. There are 4 types of operators in C, They are:
- Arithmetic operators.
- Relational operators.
- Logical operators.
- Bitwise operators.
- Assignment operators.
- Other operators.
1. Arithmetic operators
These operators are used to perform arithmetic (or) mathematical operations between variables/operands for example, +, -, *, etc. There are 2 types of Arithmetic operators, they are:Binary operators: These are used to perform operations between two operands (or) variables. They are
a) Addition operator: It is used to add two operands, represented with + (plus).
b) Subtraction operator: This operator is used to subtract one from other, represented with – (minus).
c) Multiplication operator: It is used to multiply two operands, represented with * (asterisk).
d) Division operator: Division operator is used to dividing one operand by the other, represented with / (back-slash).
Unary operators: These operators work with a single operand to increase or decrease the value. They are
a) Increment operator: It is used to increase the value of operand by 1, represented with ++.
b) Decrement operator: It is used to decrease the value of operand by 1, represented with –.
Program 1: using Arithmetic operators in C.
#include<stdio.h>
void main(){
//intialization
int a=10,b=20,c;
//Addition Operator
c=a+b;
printf("\n Addition is %d", c);
//Subtraction Operator
c=a-b;
printf("\n Subtraction is %d",c);
//Multiplication Operator
c=a*b;
printf("\n Product is %d", c);
//Division Operator
c=a/b;
printf("\n Division is %d",c);
}
OUTPUT:
10 is greater than 5
10 is greater than or equal to 10
10 is less than or equal to 10
10 is not equal to 5
2. Relational operators
These operators are used to compare two operands for checking which operator is greater than or equal to other operators. For example, “>” is used to compare which is greater in two. There are 6 types of Relational operators they are:
- Equal to (==): This operator is used to compare two operands are equal or not. For example, 999 == 999 then it returns TRUE. It is represented using ” == “.
- Greater than (>): Greater than operator used to compare which value is greater among the operands. For example, 10 > 4 which gives TRUE as a result.
- Lesser than (<): Lesser than operator is used to compare which value is lesser among the operands. For example, 10 < 4 which gives FALSE as a result that returns 0.
- Greater than or equal to (>=): It is used to compare greater than or equal to values among the operands. For example, 10>=10 returns TRUE as 10 is equal to 10.
- Lesser than or equal to (<=): This operator Lesser than or equal to is used to compare lesser than or equal to values among the operands. For example, 10<=9 returns TRUE as 9 is less to 10.
- Not equal to (!=): Not Equal to operator (!=) is used to check whether the two operands are equal to each other or not. For example, 10 != 9, returns TRUE as 10 not equals to 9
Program 2: using Relational operators in C.
//Program to use different arithmetic operators
#include<stdio.h>
void main{
//intitialization
int a=10, b=5, c=10;
//equal to operator
if(a==b){
printf("\n a is equal to b");
}
//Greater than operator
if(a>b){
printf("\na is greater than b");
}
//Lesser than operator
if(a<b){
printf("\n a is lesser then b");
}
//Greater than or equal to
if(a>=b){
printf("\n a is greater than or equal to b");
}
//Leeser than or equal to b
if(a<=c){
printf("\n a is lesser than or equal to b");
}
//Not equal operator
if(a!=b){
printf("\n a is not equal to b");
}
}
OUTPUT:
a is greater than b
a is greater than equal to b
a is lesser than or equal to c
a is not equal to b
3. Logical operators
These operators are used to combine two (or) more instructions to a single and gives a boolean value as a result. 1 (or) 2 be the result of any statement using logical operators. These operators are used commonly for decision-making programs. There are three types of logical operators, They are:
- AND operator: The AND operator combines the statements and returns true only if all the statements are true. For example, if a=1, b=0 it returns FALSE that is returns 0. It is represented with “&&”.
- OR operator: It is used to combine the statements and return true even if any one of the statements is true. For example, a=0, b=1 it returns TRUE that is 1, as b=1 is true. It is represented with “||”.
- NOT operator: This operator is used to return the true only if the operand is 0. otherwise, returns FALSE. For example, if (!a=0), it returns TRUE that is 1. It is represented with “!”.
Program 3: using Logical operators in C.
//Logical Operators in C Language
#include<stdio.h>
void main(){
//Initialization
int a=10, b=5, c=2;
//And Operator
if((a>b)&&(a>c)){
printf("\n a is the biggest number");
}
//OR Operator
if((a<b)||(a>b)){
printf("\n Results come out if anyone of the condition is true");
}
//NOT Operator
if(a!=b){
printf("\n a is not equal to b");
}
}
a is the biggest number
Results come out if anyone of the condition is satisfied
a is not equal to b
4. Bitwise operators
These operators are a little different from all other operators as these work at a bit level of the operands. The actions performed by these operators are able to change the bits.
*BIT: A bit is the smallest amount of data which can be stored either it is 0 (or) 1. 8 bits together form a byte.
Types of bitwise operators:
- Bitwise And (&): The AND operator compares each bit of 2 numbers (or) values. Returns 1 if both the bits of either number is 1 or else returns 0. It is represented with “&”.
- Bitwise OR (|): The OR operator does the or operation for each bit of two numbers (or) values. It is represented with “|”.
- Bitwise XOR (^): XOR operator does the xor operation for each bit of two numbers (or) values. It is represented with “^”.
- Bitwise NOT(~): The bitwise NOT operator inverts all the bits of given value (or) number. If it is 1 then it turns to 0 and vice versa. It is represented with “~”.
- Bitwise LEFT SHIFT(<<): The bitwise LEFT SHIFT operator shifts the bits of a number to 1 unit left. That is second place bit comes to first place. It is represented with “<<“.
- Bitwise RIGHT SHIFT(>>): The bitwise RIGHT SHIFT operator shifts the bits of a number to 1 unit right. That is first place bit comes to second place and first place holds 0 in it. It is represented with “>>”.
Program 4: using BITWISE operators in C.
//Program using BITWISE Operators in C
#include<stdio.h>
void main(){
//Initializing Variables
int a=10, b=5;
printf("\n a=%d, b=%d",&a,%b);
//BITWISE AND
printf("\n a & b is %d", (a&b));
//BITWISE OR
printf("\n a | b is %d", (a|b));
//BITWISE XOR
printf("\n a ^ b is %d",(a^b));
//BITWISE NOT
printf("\n ~b is %d ", (~b));
//BITWISE Left Shift
printf("\n a<<b is %d",(a<<b));
//BITWISE Right Shift
printf("\n a>>b is %d",(a>>b));
}
OUTPUT:
a=10, b=5
a & b is 0
a | b is 15
a ^ b is 15
~b is -6
a << b is 320
a>>b is 0
5. Assignment operators
As the name suggests, Assignment operators assign the value which is on the right side to the left side. For example, “=” n1 = 10 which means 10 is assigned to the n1 variable. These types of operators are known as assignment operators.
- = – The equal to (=) operator assigns the value of the right side of it to the left side.For example, n1 = 10, now the value of n1 is 10.
- += – It is a combined operator of addition and equal to operators. First, it adds the value of the right to left and equals the sum to the left side one generally, a variable. For example, n1 += 10, this is considered as n1 = n1+10.
- -= – This operator is a combined operator of subtraction and equal to operators. Firstly, it subtracts the value of the right to left and equals the difference to the left side one generally, a variable. For example, n1 -= 10, this is considered as n1 = n1-10.
- *= – It is a combined operator of multiplication and equal to operators. First, it multiplies the value of the right to left and equals the product to the left side one generally, a variable. For example, n1 *= 10, this is considered as n1 = n1*10.
-
/= – This operator is a combined operator of division and equal to operators. Firstly, it divides the value of the right to left and equals the sum to the left side one generally, a variable. For example, n1 /= 10, this is considered as n1 = n1/10.
Program 5: using ASSIGNMENT operators in C.
//Program using Assignment Operators in C
#include<stdio.h>
void main(){
//Initialization
int a,b;
a=10,b=5;
printf("\n a=%d, b=%d", a, b);
//a = a + b
a+= b;
printf("\n a+= b is %d ", a);
//a = a - b
a=10, b=5;
a-= b;
printf("\n a- = b is %d", a);
//a = a * b
a=10, b=5;
a*= b;
printf("\n a* = b is %d", a);
//a = a/b
a=10, b=5;
a/= b;
printf("\n a/ = b is %d", a);
}
a=10,b=5
a+ = b is 15
a- = b is 5
a* = b is 20
a/= b is 2
6. Other operators
Other operators (or) miscellaneous operators are a special type of operators that are used to perform certain functions. These operators in C programming used when we need to perform a different type of operation not like a routine as in the above list. There are many operators in this category, in those some frequently used are:
- sizeof(): This is a special operator that is used to know the size of any kind of operator. For example, the size of int is 2 bytes and sizeof(int) gives the result as 2. Hence, it is useful to know the size of the operators.
- comma(,): It is used to separate two values. For example, int n1, int n2.
- Ternary operator: It is an operator used instead of an if-else statement. Syntax follows as (expression)? True_value:False_value;. It evaluates the expression and executes TRUE statement if the expression is true vice versa.
Program 6: using OTHER operators in C.
//Program using OTHER operators in C
#include<stdio.h>
void main(){
//Initialization
int a,b;
//Comma operator
a=10, b=5;
//Sizeof() operator
printf("\n Size of int is %d",sizeof(a));
//ternary operator
(4>0)?printf("TRUE"):printf("FALSE");
}
Size of int is 4
TRUE
IF YOU LIKE IT FOLLOW US HERE !!!!!!!