Follow

Follow

# Operators in C Programming

Rishabh Kumar
·Jan 30, 2022·

• What is Operator
• Video Explaination

# What is Operator

An `Operator` in computer programming is a symbol that helps us to perform mathematical and logical operations. We have 6 types of operators in C

• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Misc Operators

# Video Explaination

Now we will learn every type of operator in detail. Let's get started :

## Arithmetic Operators

As the name suggests `Arithmetic Operators` helps us to perform arithmetic operations in c Programming. Look at the table to get more ideas about Arithmetic Operators.

### Sample Code

``````#include <stdio.h>
int main()
{
int a = 36;
int b = 5;
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a %% b = %d\n", a % b);
return 0;
}

// Output
a + b = 41
a - b = 31
a * b = 180
a / b = 7
a % b = 1
``````

## Relational Operators

Relational Operators helps us to figure out certain relations between two operands. It returns true or false based on whether the condition is true or false. And in the C language true and false are represented by `1 and 0` respectively. Let's understand how to work with Relational Operators using the table below.

### Sample Code

``````#include <stdio.h>
int main()
{
int a = 36;
int b = 5;
int c = 5;
printf("%d\n", a == b);
printf("%d\n", b == c);
return 0;
}
// Output
0
1
``````

## Logical Operators

Logical Operators are used to checking the `and`, `or` and `not` conditions between two statements or operands. Let's look at the table.

### Sample Code

``````#include <stdio.h>
int main()
{
int a = 36;
int b = 5;
int c = 0;
printf("%d\n", a && b);
printf("%d\n", b && c);
printf("%d\n", b || c);
return 0;
}
``````

## Bitwise Operators

As the name suggests bitwise operator returns the value by comparing two operands bit by bit after converting it to binary.

Let's understand with an example suppose a = 5, and b = 7; In binary A = 101 // 5 In binary B = 111 // 7 in binary Let's calculate A & B

Starting from right in A we have 1 and in b we have 1 it will return 1. Second place A it's 0 B it's 1 hence 0. Now third and last place it's 1 in A and 1 in B so the result will be 1. Finally, we have our result as 101 which again in decimal would be 5.

So A & B will result in 5.

There are some more bitwise operators but we don't use them usually. These are One's Complement Operator `~`, Binary Left Shift Operator `<<` and Binary Right Shift Operator `>>`.

### Sample Code

``````#include <stdio.h>
int main()
{
int a = 5;
int b = 7;
int c = a & b;
printf("%d", c);
return 0;
}
// Output
5
``````

## Assignment Operators

Simple Assignment Operators is `=` which is used to assign values to variables in a programming language. for example. `a = 3`. We are assigning value to 3 to variable a. but assignment variable can also be used with `Arithmetic` and `Bitwise` operators let's look at the example.

### Sample Code

``````#include <stdio.h>
int main()
{
int a = 5;
a+=3; // adds number 3 in a then assign result to a.
printf("a = %d", a);
return 0;
}
// Outout
a = 8
``````

## Misc Operators

There are also some miscellaneous operators in C.

### Sample code

``````#include <stdio.h>
int main()
{
int a = 36;
int c = 20;
printf("%d\n", sizeof(a)); // return size of variable a
printf("%d\n", &a); // return address of a
int *b = &a; // b is pointer to a memory location
*b = 7; // changing value of a using pointer
printf("%d\n", a); // printing new value of a
int d;
d = (a>c) ? 100 : 200; // assigning value to d based on condition
printf("%d\n", d); // getting value of c
return 0;
}
//Output
4
6422024
7
200
``````

These are all the operators we can use in c to write programs. I hope I have cleared all the doubts related to operators in C. If you still have any doubts regarding operators or this article just comment down. And do checkout my youtube channel.