Home » Relational Operator in C

Relational Operator in C

by Online Tutorials Library

Relational Operator in C

Operators are the special symbols used to perform mathematical and logical operations to the given operands. There are various types of operators in C, such as arithmetic, logical, bitwise, relational, conditional or ternary, etc.

Relational Operator in C

Relational Operators are the operators used to create a relationship and compare the values of two operands.

For example, there are two numbers, 5 and 15, and we can get the greatest number using the greater than operator (>) that returns 15 as the greatest or larger number to the 5.

Types of Relational Operators

Following are the various types of relational operators in C.

Equal To Operator (==)

It is used to compare both operands and returns 1 if both are equal or the same, and 0 represents the operands that are not equal.

Syntax

Let’s create a program to use the double equal to operator (==) for comparing the operands value in C.

Program1.c

Output

a == b : 0   5 is not equal to 10   x == y : 1   5 is not equal to 5  

Not Equal To Operator (!=)

The Not Equal To Operator is the opposite of the Equal To Operator and is represented as the (!=) operator. The Not Equal To Operator compares two operands and returns 1 if both operands are not the same; otherwise, it returns 0.

Syntax:

Let’s create a simple program to use the Not equal to (!=) operator for comparing the values of variable in C.

Program2.c

Output

a != b : 1   5 is equal to 10   x != y : 0   5 is equal to 5  

Less than Operator (<)

It is used to check whether the value of the left operand is less than the right operand, and if the statement is true, the operator is known as the Less than Operator.

Syntax:

Here the operand A is less than operand B.

Let’s create a program to use the less-than operator (<) to compare the operand value in C.

Program3.c

Output

Enter the value of num1: 45     Enter the value of num2: 89     The value of num1 is less than num2.  

Greater than Operator (>)

The operator checks the value of the left operand is greater than the right operand, and if the statement is true, the operator is said to be the Greater Than Operator.

Syntax:

Here, operand A is greater than operand B.

Let’s create a program to use the greater than operator (>) to compare the operand value in C.

Program4.c

Output

Enter the value of num1: 20     Enter the value of num2: 30     The value of num2 is greater than num1.  

Less than Equal To Operator (<=)

The operator checks whether the value of the left operand is less than or equal to the right operand, and if the statement is true, the operator is said to be the Less than Equal To Operator.

Syntax:

Here, operand A is less than or equal to operand B. So, it is a Less than Equal to Operator.

Let’s create a program to use the Less than Equal to operator (<=) to compare the operand value in C.

Program5.c

Output

Enter the value of num1: 45     Enter the value of num2: 45     The value of num1 is equal to num2.  

Greater than Equal To Operator (>=)

The operator checks whether the left operand’s value is greater than or equal to the right operand. If the statement is true, the operator is said to be the Greater than Equal to Operator.

Syntax:

Here, operand A is greater than equal to the right operand B. So, it is the Greater than Equal To operator.

Let’s create a program to use the Greater than Equal To operator to compare the operand value in C.

Program6.c

Output

Enter the value of num1: 28     Enter the value of num2: 79     The value of num2 is greater than num1.  

You may also like