Login
Login

Operators in C Language

Spread the love

Last Updated on July 12, 2021 by Rahul

Operators in C Language

‘Welcome in Study Learner

Today i will teach you about different opeartors in C programming with the help of examples.

An operator is a symbol that tails the compiler to perform specific logical or mathematical functions. C language provides the following types of operators:-

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. Increment/Decrement Operators
  7. Conditional Operators
  8. Special Operators

Arithmetic Operators :-

Arithmetic operators will used to perform basic mathematical operation like Addition(+), Substraction(-), Multiplication(*), Division(/) and Modulus(%).

The following table show all the arithmetic operators with example.
Assume variable A holds 30 and variable B holds 60 then:-

Operator TypeOperator OperationExample
Addition +Add two operands.A + B = 90
SubtractionSubtract second operand from first operand.B – A = 30
Multiplication *Multiply both operands.A * B = 180
Division /Divide numerator by de-numerator.B / A = 2
Modulus %Modulus operator and remainder of after an integer division.B % A = 0

Create a simple C program to understand Arithmetic operators.

#include<stdio.h>
int main()
{
	int a,b,c;
	printf("Enter the value of A:");
	scanf("%d",&a);
	printf("Enter the value of B:");
	scanf("%d", &b);
	printf("A = %d\n", a);
	printf("B = %d\n\n", b);

	//preform Addition operation
	c  = a + b;
	printf("A + B = %d\n", c);

	//perform Subtract operation
	c = b - a;
	printf("B - A = %d\n", c);

	//perform Multiply operation
	c = a * b;
	printf("A * B = %d\n", c);

	//perform division operation
	c = b / a;
	printf("B / A = %d\n", c);

	//perform modulus operation
	c = b % a;
	printf("B %% A = %d\n", c);
        return 0;
}
Output:-

Output

Relational Operators :-

Relational operators will used to perform comparison on two operands. They are important for making decisions. They allow us compare numeric and char values to determine if one is greater than, less than, equal to, or not equal to another. It is always produce ‘Boolean Result(T/F)’.

The following table show all the Relational operators with example.

Assume variable A holds 40 and variable B holds 50 then:−

Operator TypeOperator OperationExample
Greater than >Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true.(A > B) is True.
Less than <Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true.(A > B) is not True.
Greater than or equal to >=Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true.(A >= B) is True.
Less than or equal to <=Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true.(A <= B) is not True.
Equal to ==Checks if the values of two operands are equal or not. If yes, then the condition becomes true.(A == B) is not True.
Not equal to !=Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true.(A != B) is True.

Create simple C programs to understand Relational operators.

1.Grater than(>) Operator:-
#include<stdio.h>
int main()
{
	int a, b, c;
	printf("Enter the value of A:");
	scanf("%d", &a);
	printf("Enter the value of B:");
	scanf("%d", &b);
	printf("A = %d\n", a);
	printf("B = %d\n", b);
	if(a>b)
	{
		printf("A is grater than B \n");
	}
	else
	{
		printf("A is not grater than B\n");
	}
        return 0;
}
Output:-

Output

2. Less than(<) Operator:-
#include<stdio.h>
int main()
{
	int a, b, c;
	printf("Enter the value of A:");
	scanf("%d", &a);
	printf("Enter the value of B:");
	scanf("%d", &b);
	printf("A = %d\n", a);
	printf("B = %d\n", b);
	if(a<b)
	{
		printf("A is less than B \n");
	}
	else
	{
		printf("A is not less than B\n");
	}
        return 0;
}
Output:-

Output

3. Grater than or equal to(>=) Operator:-
#include<stdio.h>
int main()
{
	int a, b,c;
	printf("Enter the value of A:");
	scanf("%d", &a);
	printf("Enter the value of B:");
	scanf("%d", &b);
	printf("A = %d\n", a);
	printf("B = %d\n", b);
	if(a >= b)
	{
		printf("A is either grater than or equal to B\n");
	}
	else
	{
		printf("A is neither grater than nor equal to B\n");
	}
        return 0;
}
Output:-

Output

4. Less than or equal to(<=) Operator:-
#include<stdio.h>
int main()
{
	int a, b,c;
	printf("Enter the value of A:");
	scanf("%d", &a);
	printf("Enter the value of B:");
	scanf("%d", &b);
	printf("A = %d\n", a);
	printf("B = %d\n", b);
	if(a <= b)
	{
		printf("A is either less than or equal to B\n");
	}
	else
	{
		printf("A is neither less than nor equal to B\n");
	}
        return 0;
}
Output:-

Output

5. Equal to(==) Operator:-
#include<stdio.h>
int main()
{
	int a, b,c;
	printf("Enter the value of A:");
	scanf("%d", &a);
	printf("Enter the value of B:");
	scanf("%d", &b);
	printf("A = %d\n", a);
	printf("B = %d\n", b);
	if(a==b)
	{
		printf("A is equal to B\n");
	}
	else
	{
		printf("A is not equal to B\n");
	}
        return 0;
}
Output:-

Output

6. Not equal to(!=) Operator:-
#include<stdio.h>
int main()
{
	int a, b,c;
	printf("Enter the value of A:");
	scanf("%d", &a);
	printf("Enter the value of B:");
	scanf("%d", &b);
	printf("A = %d\n", a);
	printf("B = %d\n", b);
	if(a!=b)
	{
		printf("A is not equal to B\n");
	}
	else
	{
		printf("A is equal to B\n");
	}
        return 0;
}
Output:-

Output

Enter the value of A:10
Enter the value of B:12
A = 10
B = 12
A is not equal to B

Enter the value of A:10
Enter the value of B:10
A = 10
B = 10
A is equal to B

Logical Operator :-

Logical operators will used to combine result of two relational expressions.logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming.

The following table show all the Logical operators with example.

Operator TypeOperator OperationExample
Logical AND &&True only if all operands are trueIf a = 3, b = 2 and c = 1 then, expression (a>b) && (c<a) is equals to 1.
Logical OR ||True only if either one operand is trueIf a = 1, b = 2 and c = 3 then, expression (a==b) || (c>a) is equals to 0.
Logical NOT !True only if the operand is 0If c = 5 then, expression !(c==5) equals to 0.

Create simple C programs to understand Logical operators.

Logical AND(&&) Operator:-
#include<stdio.h>
int main()
{
	int a, b, c, d;
	printf("Enter the value of A:");
	scanf("%d", &a);
	printf("Enter the value of B:");
	scanf("%d", &b);
	printf("Enter the value of C:");
	scanf("%d", &c);
	d = (a > b) && (c < a);
	printf("(a > b) && (c < a) is %d\n", d);
        return 0;
}
Output:-

Output

Logical OR(||) Operator:-
#include<stdio.h>
int main()
{
	int a, b, c, d;
	printf("Enter the value of A:");
	scanf("%d", &a);
	printf("Enter the value of B:");
	scanf("%d", &b);
	printf("Enter the value of C:");
	scanf("%d", &c);
	d = (a == b) || (a > c);
	printf("(a == b) || (c > a) is %d\n", d);
        return 0;
}
Output:-

Output

Enter the value of A:10
Enter the value of B:20    
Enter the value of C:5
(a == b) || (c > a) is 1

Enter the value of A:10
Enter the value of B:20
Enter the value of C:30
(a == b) || (c > a) is 0
Logical NOT(!) Operator-
#include<stdio.h>
int main()
{
	int a, b, c;
	printf("Enter the value of A:");
	scanf("%d", &a);
	printf("Enter the value of B:");
	scanf("%d", &b);
	c = !(a == b);
	printf("!(a == b) is %d\n", c);
	return 0;
}
Output:-

Output

Bitwise Operators :-

Bitwise operators are special operator set provided by ‘C.’ They are used in bit level programming. These operators are used to manipulate bits of an integer expression.

The following table show all the Bitwise operators with example.

Operator TypeOperator MeaningExample
Bitwise AND &Bitwise AND operatorA = 20
B = 21
And = 20
Bitwise exclusive OR ^Bitwise exclusive OR operatorA = 20
B = 21
Exclusive-OR = 1
Bitwise OR |Bitwise OR operatorA = 20
B = 21
OR = 21
Shift right >>Right shift operatorA = 20
Right shift = 5
Shift left <<Left shift operatorA = 20
Left shift = 80
Bitwise complement ~Binary One’s Complement Operator is a unary operatorA = 10
complement = -11

Create a simple C program to understand Bitwise operators.


#include<stdio.h>
int main()
{
	int a=20;   // 20 = 010100
        int b=21;   // 21 = 010101
        int c;
	printf("A = %d\n", a);
	printf("B = %d\n", b);
	c = a & b;   // 20 = 010100
	printf("And = %d\n", c);
	c = a ^ b;   // 1 = 0001
	printf("Exclusive-OR = %d\n",c);
	c = a | b;   // 21 = 010101
	printf("OR = %d\n", c);
	a=20;     // 20 = 010100
	printf("A = %d\n", a);
	c = a >> 2;  // 5 = 000101
	printf("Right shift = %d\n", c);
	c = a << 2;  // 80 = 101000
	printf("Left shift = %d\n", c);
	a=10;  // 10 = 1010
	printf("A = %d\n", a);
	c = ~(a);
	printf("complement = %d\n", c);
	return 0;
}
Output:-

Output

Assignment Operators:-

An assignment operator is used for assigning a value to a variable of expression.

The following table show all the Assignment operators with example.

Operator TypeOperator OperationExample
Simple assignment operator =Assigns values from right side operands to left side operand.C = A + B will assign the value of A + B to C
Add AND assignment operator +=It adds the right operand to the left operand and assign the result to the left operand.C += B is equivalent to C = C + B
Subtract AND assignment operator -=It subtracts the right operand from the left operand and assigns the result to the left operand.B -= A is equivalent to B = B – A
Multiply AND assignment operator *=It multiplies the right operand with the left operand and assigns the result to the left operand.C *= A is equivalent to C = C * A
Divide AND assignment operator /=It divides the left operand with the right operand and assigns the result to the left operand.C /= B is equivalent to C = C / B
Modulus AND assignment operator %=It takes modulus using two operands and assigns theC %= A is equivalent to C = C % A

Create a simple C program to understand Assignment operators.

#include<stdio.h>
int main()
{
	int a, b, c;
	printf("Enter the value of A:");
	scanf("%d", &a);
	printf("Value of A = %d\n", a);
	
	//simple assignment operator
	c = a;
	printf("c =  %d\n", c);

	//Add AND assignment operator
	c += a;
	printf("c = %d\n", c);

	//Subtract AND assignment operator
	c -= a;
	printf("c = %d\n", c);

	//Multiply AND assignment operator
	c *= a;
	printf("c = %d\n", c);

	//Divide AND assignment operator
	c /= a;
	printf("c = %d\n", c);

	//Modulus AND assignment operator
	c %= a;
	printf("c = %d\n", c);
	return 0;
}
Output:-

Output

Increment/Decrement Operators:-

Increment operator is used to increase the value of variable by one and represented by ++. Decrement operator is used to decrease the value of variable by one and represented by –.

The following table show the Increment/Decrement operators with example.

Operator TypeOperator OperationExample
Increment operator ++increase the value of variable by one.a = 4
b = ++a
b = 5
Decrement operatordecrease the value of variable by one.a = 6
b = –a
b = 5

Create a simple C program to understand Assignment operators.

#include<stdio.h>
int main()
{
	int a,b;
	printf("Enter the value of A:");
	scanf("%d", &a);
	printf("Value of A = %d\n", a);
	//increment operator
	b = ++a;
	printf("B = %d\n", b);

	//decrement operator
	b = --a;
	printf("B = %d\n", b);
	return 0;
}
Output:-

Output

Conditional Operators:-

The conditional operator is kind of similar to the if-else statement as it does follow the same algorithm as of if-else statement but the conditional operator takes less space and helps to write the if-else statements in the shortest way possible.

Create a simple C program to understand Conditional operators.

#include<stdio.h>
int main()
{

	int a,b;
	printf("Enter the value of A:");
	scanf("%d", &a);
	printf("Enter the value of B:");
	scanf("%d", &b);
	printf("Value of A = %d\n", a);
	printf("Value of B = %d\n", b);
	(a > b) ? printf("A is greater than B that is %d > %d", a, b)
                : printf("B is greater than A that is %d > %d",b, a);

        return 0;
}

output:-

Output

Special Operators:-

The following table show all the Special operators with example.

OperatorOperationExample
sizeofReturns the size of an variablesizeof(A) return size of the variable A
&Returns the address of an variable&A ; return address of the variable A
*Pointer to a variable*A ; will be pointer to a variable A

Create a simple C program to understand & and * operator.

#include<stdio.h>
int main()
{
	int *ptr, a;
	printf("Enter the value of A:");
	scanf("%d", &a);
	printf("Value of A = %d\n", a);
	//address of a is assigned to ptr
	ptr = &a;
	//display the value of a using ptr variable
	printf("%d", *ptr);
	return 0;
}
Output:-

Output

Create a simple C program to understand sizeof operator.

#include<stdio.h>
int main()
{
	int a;
	char b;
	float c;
	double d;
	printf("Storage size for integer data type = %d\n", sizeof(a));
	printf("Storage size for char data type = %d\n", sizeof(b));
	printf("Storage size for float data type = %d\n", sizeof(c));
	printf("Storage size for double data type = %d\n", sizeof(d));
	return 0;
}
Output:-

Output


Spread the love

1 thought on “Operators in C Language”

Leave a Comment


error: Content is protected !!