08

Oct**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

Arithmetic operators

Relational operators

Logical operators

Bitwise operators

Assignment operators

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.

### Arithmetic Operators Example

#include <stdio.h> #include <conio.h> void main() { int a b ,c; clrscr(); // Clear the screen a=10; b=3; c = a+b; printf("Total (a+b) = %d \n",c); c = a-b; printf("Sub (a-b )= %d \n",c); c = a*b; printf("Mul (a*b) = %d \n",c); c=a/b; printf("Div (a/b) = %d \n",c); c=a%b; printf("Remainder (a Mod b) = %d \n",c); 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

### Relational Operators Example

#include <stdio.h> #include <conio.h> void main() { int a = 10, b = 20, c = 30; clrscr(); printf("%d == %d result %d \n", a, b, a == b); printf("%d == %d result %d \n", a, c, a == c); printf("%d > %d result %d \n", a, b, a > b); printf("%d > %d result %d \n", a, c, a > c); printf("%d < %d result %d \n", a, b, a < b); printf("%d < %d result %d \n", a, c, a < c); printf("%d != %d result %d \n", a, b, a != b); printf("%d != %d result %d \n", a, c, a != c); printf("%d >= %d result %d \n", a, b, a >= b); printf("%d >= %d result %d \n", a, c, a >= c); printf("%d <= %d result %d \n", a, b, a <= b); printf("%d <= %d result %d \n", a, c, a <= c); getch(); }

**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 thenThese operators are used to perform logical operations on the given two variables.

### Logical Operators Example

#include <stdio.h> #include <conio.h> void main() { int a = 30,b =10, c=10; clrscr(); if(a>b && a>c ) printf("\n a is grather then b and c "); else printf("\n a is not grather then b and c "); 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

### Bitwise Operators Example

#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

### Assignment Operators Example

#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

## Type Information Operators (Special operators)

These operators are used to provides information about a particular type. The following is a table of type information operators in C.

### Sizeof Operator Example

#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(); }

## Misc. Operators

There are some more important operators supported by C. The following is a table of some Misc operators in C. Suppose you have two integer variables X, Y and having values 5, 2 then

### Misc. Operators Example

#include <stdio.h> #include <conio.h> void main() { int a = 10,b = 20 ,m ; clrscr(); m= a>b? a+b: a+10; printf("Result = %d\n",m); getch(); }

**Output**

Result = 20

#include <stdio.h> #include <conio.h> void main() { int *ptr, q; q = 10; ptr = &q; printf("%d", *ptr); getch(); }

**Output**

Storage size of data type int is :4 Storage size of data type char is :1 Storage size of data type float is :4 Storage size of data type double is :8

**Read More:**

**- Conditional Statements : if, else, switch **

## Take our free skill tests to evaluate your skill!

In less than 5 minutes, with our skill test, you can identify your knowledge gaps and strengths.