Types of operators
6 minute read
Types of operators
Explain operators in c language.There are eight types of operator in c language
1. arithmetic operators
There are five arithmetic operators.
Ø + Addition or unary plus
Ø - Subtraction or unary minus
Ø * Multiplication
Ø / Division
Ø % Modulo operator
Program
#include<stdio.h>
#include<conio.h>
void main ()
{
// Variable Declaration
Int a=100;
Int b=2;
Int result;
result = a - b; // subtraction
printf ("a - b = %d”, result);
}
Output
a - b = 98
2. Relational operator
It Compare two operands and depending upon their relation.
There are six relational operators. They are,
Ø < less than
Ø > greater than
Ø <= less than or equal to
Ø >= greater than or equal to
Ø == equal to
Ø != not equal to
Program
#include<stdio.h>
#include<conio.h>
void main()
{
// Variable Declaration
int a = 25, b = 5;
if( a>b )
printf (A is Big);
else
printf ( B is Big);
// Wait For Output Screen
getch();
}
Output
A is Big.
3. Logical Operators
AND,OR operators are used when we want to use two or more Conditions.
Types Of Logical Operators
Ø && Logical AND
Ø || Logical OR
Ø ! Logical NOT
Logical And (&&) Operator
If both the conditions are true, then the operation becomes true.
Syntax
expr1 && expr2
Logical And Operator Example
if( (a>10) && (a<20) )
printf(A is between 10 and 20);
Logical And Operator Example
#include<stdio.h>
#include<conio.h>
void main()
{
// Variable Declaration
int num1 = 50;
int num2 = 40;
if(num1>=40 && num2>=40){
printf("both condition are true");
}
// Wait For Output Screen
getch();
}
Output:
Both condition are true
Logical OR (||) Operator
Logical OR Operator Definition
If any one of the condition is successful, then the operation becomes true
Syntax
expr1 || expr2
Example
if( (a>
10)
|| (a<
20) )
printf(
A is greater than
10
or less than
20);
Logical OR Operator Example
#include<stdio.h>
#include<conio.h>
void main()
{
// Variable Declaration
int num1 = 30;
int num2 = 40;
if(num1>=40 || num2>=40){
printf("From two condition one condition is true");
}
// Wait For Output Screen
getch();
}
Output
From two condition one condition is true
Logical NOT (!) Operator
If a condition is true, Logical NOT operator reverses the result and produces false output.
Syntax
!expression
Example
if( !(a>10))
printf(A is less than 10 );
Program
#include<stdio.h>
#include<conio.h>
int main()
{
int number;
printf("Please enter an integer value:");
scanf("%d", &number);
if(!(number>40)){
printf("no is greater than 40 - TRUE");
}else{
printf("no is greater than 40 - FALSE");
}
// Wait For Output Screen
getch();
return 0;
}
Output
Please enter an integer value: 36
no is greater than 40 - FALSE
4. Assignment Operators
It is used to assign the result of an expression to a variable.
Syntax
identifier = Expression
There are five assignment operators. They are,
a=a+1 a+=1
a=a- 1 a-= 1
a=a*2 a*=2
a=a/2 a/= 2
a=a%2 a%=2
Program
#include <stdio.h>
#include<conio.h>
void main(){
int a = 10;
// = Operator
int b = a;
printf("b = %d\n",b);
// += Operator
b += 10;
printf("b += 10;b = %d\n",b);
// -= Operator
b -=5;
printf("b -=5;b = %d\n",b);
// *= Operator
b *=4;
printf("b *=4;b = %d\n",b);
// /= Operator
b /=2;
printf("b /=2;b = %d\n",b);
}
Output:
b = 10
b += 10;b = 20
b -=5;b = 15
b *=4;b = 60
b /=2;b = 30
5. Increment or Decrement Operators
Increment or Decrement operator are also called unary increment or unary decrement.
Increment Unary Operator
variable++
++variable
Is Equivalent i=i+1
Increment Unary Operator Types
· Post Increment i++
· Pre Increment ++i
Decrement Unary Operator
variable
--;
--variable;
Is Equivalent i=i-1
Decrement Unary Operator Types
· Post Decrement i--
· Pre Decrement --i
Unary Operators Explanation
· ++i : it increase the l and then uses its value as the value of the expression;
· i++ : uses l as the value of the expression and then increase the l;
· --i : it decrease the l and then uses its value as the value of the expression;
· i-- : uses l as the value of the expression and then decrease the l.
Example Program
#include<stdio.h>
#include<conio.h>
int main()
{
int a;
/* Increment Operators */
a = 5;
printf("Post Increment = %d\n",a++);
a = 5;
printf("Pre Increment = %d\n",++a);
/* Decrement Operators */
a = 5;
printf("Post Decrement = %d\n",a--);
a = 5;
printf("Pre Decrement = %d\n",--a);
// Wait For Output Screen
getch();
//Main Function return Statement
return 0;
}
Sample Output:
Post Increment = 5
Pre Increment = 6
Post Decrement = 5
Pre Decrement = 4
6. Conditional or Ternary operator
Check the condition if it is true than it returns first variables value otherwise it return second variable value. Sometimes it replaces if..else statement
Syntax
Condition? Expression1: Expression2
Example
(a>50) ? b : c
It is also called ternary operators.
Example Program
#include <stdio.h>
#include<conio.h>
void main() {
int a = 10;
int b = 15;
int c
c = a <= b ? a : b;
printf("C Is %d", c);
getch();
}
Output:
C Is 10
7. Bitwise Operators
Following are the bitwise operators.
Bit Operators
Ø & Bitwise AND
Ø | Bitwise inclusive-OR
Ø ^ Bitwise OR
Ø ~ Ones complement
Ø << Left shift
Ø >> Right shift
8. Special Operators
Ø The Comma Operator
Ø Type cast Operator
Ø Reference operator or Address Operater ("&")
Ø Dereference operator ("*") or Pointer Operater
Ø Double Pointer operator ("**")
Ø sizeof operator
Example For Comma Operator
int a,b;
Type cast Operator
· Converts to the specified type.
Example
float s= 12.5;
int a;
a = (int) s;
now variable a’s value is 12.
Reference operator or Address Operater ("&")
The reference operator noted by ampersand ("&"), is also a unary operator in c languages that uses for assign address of the variables. It returns the pointer address of the variable. This is called "referencing" operater.
Syntax
data_type x;
data_type *pt;
pt = &x
Dereference operator ("*") or Pointer Operater
The dereference operator or indirection operator, represent by asterisk ("*"). It is also called unary operator in c languages that uses for pointer variables.
data_type *pt;
Double Pointer operator ("**")
Double pointer points to another pointer variable address.
data_type **pt;
sizeof operator
It gives the size of a variable.
sizeof (data_type)
Example
sizeof(int)
output
2