Operators in C
Operators in C
C language supports a rich set of built-in operators. An operator is a special symbol that tells
the compiler to perform specific mathematical or logical operations. Operators in programming
languages are taken from mathematics.
Types of Operators
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Bitwise operators
5. Assignment operators
6. Type Information Operators(Special operators)
Arithmetic Operators
An operator is a special symbol that tells the compiler to perform specific mathematical or
logical operations. Operators in programming languages are taken from mathematics.
Operators
Name
C Example
+
Addition
X + Y will give 7
-
Substraction
X - Y will give 3
*
Multimlication
X * Y will give 10
/
Divition
X / Y will give 2
%
Modulus
X % Y will give 1
++
Preincrement and Postincrement
X = ++Y, X = Y++ will give 3, 2
--
Predecrement and Postdecrement
X = --Y, X = Y-- will give 4, 2
#include <stdio.h>
#include <conio.h>
void main()
int a b ,c;
a=10;
b=3;
c = a+b;
c = a-b;
printf("Sub (a-b )= %d \n",c);
c = a*b;
c=a/b;
c=a%b;
getch();
Output
Total (a+b) = 13
Sub (a-b ) = 7
Mul (a*b) = 30
Div (a/b) = 3
Remainder (a Mod b) = 1
Relational operators
These operators are used to compare values and always result in boolean value (True or False).
The following is a table of relational operators in C. Suppose you have two integer variables
X, Y and having values 5, 2 respectively then
Operators
Name
C Example
>
Greater than
X > Y will give True
<
Less than
X < Y will give False
>=
Greaterthan or Equalto
X >= Y will give True
<=
Lessthan or Equalto
X <= Y will give False
>==
Equalto
X == Y will give False
!=
NotEqualto
X != Y will give True
#include <stdio.h>
#include <conio.h>
void main()
clrscr();
Output
10 == 30 result 0
10 > 20 result 0
10 > 30 result 0
10 < 20 result 1
10 < 30 result 1
10 != 20 result 1
10 != 30 result 1
10 >= 20 result 0
10 >= 30 result 0
10 <= 20 result 1
10 <= 30 result 1
Logical Operators
These operators are used to compare values and always result in boolean value (True or False).
The following is a table of logical operators in C. Suppose you have two boolean variables X,
Y and having values True, False respectively then
These operators are used to perform logical operations on the given two variables.
Operators
Name
C Example
&&
Logical AND
X && Y will give False
||
Logical OR
X || Y will give True
>!
Logical NOT
!(X), !(Y) will give False, True
#include <stdio.h>
#include <conio.h>
void main()
clrscr();
if(a>b && a>c )
else
getch();
Output
a is grather then b and c
Bitwise Operators
These operators work on bits of a binary number and perform bit by bit operation. The
following is a table of bitwise operators in C. Suppose you have two integer variables X, Y and
having values 4, 5 respectively and theirs binary equivalent would be 100, 101 then
Operators
Name
C Example
|
Bitwise OR
X | Y will give 5 i.e. 101
&
Bitwise AND
X & Y will give 4 i.e. 100
~
Bitwise NOT
~(X), ~(Y) will give -5, -6 i.e. -101, -110
^
Bitwise Exclusive OR
X^Y will give 1 i.e. 1
#include <stdio.h>
#include <conio.h>
void main()
{
int m = 10,n = 20,AND,OR,XOR,NOT ;
clrscr();
AND = (m&n);
OR = (m|n);
NOT = (~m);
XOR = (m^n);
printf("AND value = %d\n",AND );
printf("OR value = %d\n",OR );
printf("NOT value = %d\n",NOT );
printf("XOR value = %d\n",XOR );
printf("left shift value = %d\n", m << 1);
printf("right shift value = %d\n", m >> 1);
getch();
}
Output
AND value =0
OR value = 30
NOT value = -11
XOR value = 30
left shift value = 20
right shift value = 5
Assignment Operators
These operators are used to assign a new value to a variable, a property, an event, or an indexer
element. The following is a table of assignments operators in C. Suppose you have two integer
variables X, Y and having values 5, 2 then
Operators
Name
C Example
=
Equalto
X = Y will give 2
+=
Plus Equalto
X += Y will give 7
-=
Minus Equalto
X -= Y will give 3
*=
Multiply Equalto
X *= Y will give 2
/=
Divide Equato
X /= Y will give 2
%=
modulus Equalto
X %= Y will give 1
#include <stdio.h>
#include <conio.h>
void main()
{
int a ,b;
clrscr();
a=10;
b = a;
printf("b = %d \n", b);
b += a; // or b = b+a
printf("b = %d \n", b);
b -= a; // or b = b-a
printf("b = %d \n", b);
b *= a; // or b = b*a
printf("b = %d \n", b);
b /= a; // or b = b/a
printf("b = %d \n", b);
b %= a; // or b = b%a
printf("b = %d \n", b);
getch();
}
Output
b = 10
b = 20
b = 10
b = 100
b = 10
b=0
These operators are used to provides information about a particular type. The following is a
table of type information operators in C.
Operators
Description
Example
sizeof()
Returns the size of a value type.
sizeof(int) will give 2
#include <stdio.h>
#include <conio.h>
void main()
{
int a;
char b;
float c;
double d;
clrscr();
printf("Storage size of data type int is :%d \n",sizeof(a));
printf("Storage size of data type char is :%d \n",sizeof(b));
printf("Storage size of data type float is :%d \n",sizeof(c));
printf("Storage size of data type double is :%d\n",sizeof(d));
getch();
}