Hello Ninjas! Just like how we compare numbers using special symbols in mathematics, the language C also provides us with special operators that can be used to compare any two values. So today, we are going to learn about different relational operators in C and will try to code the same. Let’s learn!

Let’s start from the basics and learn about operators.

What are Operators?

Operators are ones that perform an operation on given operands. These are the special symbols that perform operations on operands.

The operators in C are categorized as -

Arithmetic Operators

Relational Operators

Assignment Operators

Logical Operators

Identity Operators

Bitwise Operators

SizeOf Operators

Conditional Operators

Comma Operators

Get the tech career you deserve, faster!

Connect with our expert counsellors to understand how to hack your way to success

User rating 4.7/5

1:1 doubt support

95% placement record

Akash Pal

Senior Software Engineer

326% Hike After Job Bootcamp

Himanshu Gusain

Programmer Analyst

32 LPA After Job Bootcamp

After Job Bootcamp

What are relational operators in C?

Relational Operators are those operators that are used to perform comparisons among operands. It returns a true or a false based on the given condition. These are essential parts of any programming language. Through relational operators, we can create logical expressions and make decisions based on the boolean results. These help in the control flow and implementation of conditions in programs.

Summary of Relational Operators in C

Operator

Description

Example

Less Than Operator (<)

Compares the two given values and returns true if the operand at left is smaller than right.

x<y

Greater Than Operator (>)

Compares the two given values and returns true if the operand at left is greater than right.

x>y

Equal To Operator(==)

Checks if both operands have equal value.

x==y

Not Equal To Operator(!=)

Checks if both operands have unequal value.

x!=y

Less Than Equal To Operator(<=)

Checks if the value of the operand on the left is less than or equal to right.

x<=y

Greater Than Equal To Operator(>=)

Checks if the value of the operand on the left is greater than or equal to right.

x>=y

Types of Relational Operators in C

The various kinds of relational operators in C are:

Less than Operator

The less than Operator is represented as ‘<’. It is used to compare the two given values and returns a true if the operand at left is smaller than the operand at right.

Syntax

The syntax of less than operator for 2 operands, say, P and Q, is:

P < Q;

Code

C

C

#include <stdio.h> #include <stdbool.h>

int main() { int operand1, operand2; bool result;

// taking input value for operand1 printf("Enter the value of numbers you want to compare :\n"); printf("Enter the value of first operand :"); scanf("%d/n", &operand1);

// taking input value for operand2 printf("Enter the value of second operand :"); scanf("%d", &operand2);

/* comparing the two operands using less than (<) operator storing the answer in variable 'result' */ result = operand1 < operand2;

// if result is true that implies operand1 is smaller than operand2 if (result == true) { printf("%d is less than %d", operand1, operand2); } // if result is false that implies operand2 is smaller than operand1 else { printf("%d is not less than %d", operand1, operand2); }

return 0; }

Output 1

Explanation

Here operand1 contains the value 53, and operand2 contains 87. As 53 is less than 87, the result variable contains a ‘true’ boolean value. Therefore, it prints the answer as “53 is less than 87”.

Output 2

Explanation

Here operand1 contains the value 93, and operand2 contains 44. As 93 is not less than 44, the result variable contains a ‘false’ boolean value. Therefore, it prints the answer as “93 is not less than 44”.

Greater than Operator

The greater than operator is represented as ‘>’. It compares the two given values and returns a true if the operand at left is greater than the operand at right.

Syntax

The syntax of greater than operator for 2 operands, say, P and Q, is:

P > Q;

Code

C

C

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

int operand1, operand2; bool result;

// taking input value for operand1 printf("Enter the value of numbers you want to compare :\n"); printf("Enter the value of first operand :"); scanf("%d/n", &operand1);

// taking input value for operand2 printf("Enter the value of second operand :"); scanf("%d", &operand2);

/* comparing the two operands using greater than (>) operator storing the answer in variable 'result' */ result = operand1 > operand2;

// if result is true that implies operand1 is greater than operand2 if (result == true) { printf("%d is greater than %d", operand1, operand2); } // if result is false that implies operand2 is greater than operand1 else { printf("%d is not greater than %d", operand1, operand2); } return 0; }

Output 1

Explanation

Here operand1 contains 98, and operand2 contains 29. As 98 is greater than 29, the result variable contains a ‘true’ boolean value. Therefore, it prints the answer as “98 is greater than 29”.

Output 2

Explanation

Here operand1 contains 9, and operand2 contains 33. As 9 is not greater than 33, the result variable contains a ‘false’ boolean value. Therefore, it prints the answer as “9 is not greater than 33”.

The equal to Operator is represented as ‘==’. It is used to check if both operands have equal value. If they contain equal values, it returns true; else, false.

Syntax

The syntax of equal to operator for 2 operands, say, P and Q, is:

P == Q;

Code

C

C

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

int operand1, operand2; bool result;

// taking input value for operand1 printf("Enter the value of numbers you want to compare :\n"); printf("Enter the value of first operand :"); scanf("%d/n", &operand1);

// taking input value for operand2 printf("Enter the value of second operand :"); scanf("%d", &operand2);

/* comparing the two operands using equal to (=) operator storing the answer in variable 'result' */ result = operand1 == operand2;

// if result is true that implies operand1 is equal to operand2 if (result == true) { printf("%d is equal to %d", operand1, operand2); } // if result is false that implies operand1 is not equal to operand2. else { printf("%d is not equal to %d", operand1, operand2); }

return 0; }

Output 1

Explanation

Here operand1 contains 17, and operand2 contains 17. As both are equal, the result variable contains a ‘true’ boolean value. Therefore, it prints the answer as “17 is equal to 17”.

Output 2

Explanation

Here operand1 contains 13, and operand2 contains 15. As both are not equal, the result variable contains a ‘false’ boolean value. Therefore, it prints the answer as “13 is not equal to 15”.

The not equal to Operator is represented as ‘!=’. It is used to check if both operands have different values. If they contain different values, it returns true else, false.

Syntax

The syntax of not equal to operator for 2 operands, say, P and Q, is:

P != Q;

Code

C

C

#include <stdio.h> #include <stdbool.h> int main() { int operand1, operand2; bool result;

// taking input value for operand1 printf("Enter the value of numbers you want to compare :\n"); printf("Enter the value of first operand :"); scanf("%d/n", &operand1);

// taking input value for operand2 printf("Enter the value of second operand :"); scanf("%d", &operand2);

/* comparing the two operands using not equal to (!=) operator storing the answer in variable 'result' */ result = operand1 != operand2;

// if result is true that implies operand1 is not equal to operand2 if (result == true) { printf("%d is not equal to %d", operand1, operand2); } // if result is false that implies operand1 is equal to operand2. else { printf("%d is equal to %d", operand1, operand2); }

return 0; }

Output 1

Explanation

Here operand1 contains 12, and operand2 contains 14. As both are not equal, the result variable contains a ‘true’ boolean value. Therefore, it prints the answer as “12 is not equal to 14”.

Output 2

Explanation

Here operand1 contains 64, and operand2 contains 64. As both are equal, the result variable contains a ‘false’ boolean value. Therefore, it prints the answer as “64 is equal to 64”.

Less than equal to Operator

Less than equal to Operator is represented as ‘<=’. It checks whether the value of the operand on the left is less than or equal to the operand on the right and returns true or false depending upon the operand's value.

Syntax

The syntax of less than equal to operator for 2 operands, say, P and Q, is:

P <= Q;

Code

C

C

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

int operand1, operand2; bool result;

// taking input value for operand1 printf("Enter the value of numbers you want to compare :\n"); printf("Enter the value of first operand :"); scanf("%d/n", &operand1);

// taking input value for operand2 printf("Enter the value of second operand :"); scanf("%d", &operand2);

/* comparing the two operands using less than equal to (<=) operator storing the answer in variable 'result' */ result = operand1 <= operand2;

// if result is true that implies operand1 is less than or equal to operand2 if (result == true) { printf("%d is less than or equal to %d", operand1, operand2); } // if the result is false that implies operand1 is not less than or equal operand2. else { printf("%d is not less than or equal to %d", operand1, operand2); }

return 0; }

Output 1

Explanation

Here operand1 contains 45, and operand2 contains 88. As 45 is less than 88, the result variable contains a ‘true’ boolean value. Therefore, it prints the answer as “45 is less than or equal to 88”.

Output 2

Explanation

Here operand1 contains 67, and operand2 contains 65. As 67 is neither less than 65 nor equal to 65, the result variable contains a ‘false’ boolean value. Therefore, it prints the answer as “67 is not less than or equal to 65”.

Greater than equal to Operator

Greater than equal to Operator is represented as ‘>=’. It checks whether the value of the operand on the left is greater than or equal to the operand on the right and returns true or false depending upon the operand’s value.

Syntax

The syntax of greater than equal to operator for 2 operands, say, P and Q, is:

P >= Q;

Code

C

C

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

int operand1, operand2; bool result;

// taking input value for operand1 printf("Enter the value of numbers you want to compare :\n"); printf("Enter the value of first operand :"); scanf("%d/n", &operand1);

// taking input value for operand2 printf("Enter the value of second operand :"); scanf("%d", &operand2);

/* comparing the two operands using greater than equal to (>=) operator storing the answer in variable 'result' */ result = operand1 >= operand2;

// if result is true that implies operand1 is greater than or equal to operand2 if (result == true) { printf("%d is greater than or equal to %d", operand1, operand2); } // if result is false that implies operand1 is not greater than or equal operand2. else { printf("%d is not greater than or equal to %d", operand1, operand2); }

return 0; }

Output 1

Explanation

Here operand1 contains 40, and operand2 contains 2. As 40 is greater than 2, the result variable contains a ‘true’ boolean value. Therefore, it prints the answer as “40 is greater than or equal to 2”.

Output 2

Explanation

Here operand1 contains -2, and operand2 contains 6. As -2 is neither greater than 6 nor equal to 6, the result variable contains a ‘false’ boolean value. Therefore, it prints the answer as “-2 is not greater than or equal to 6”.

In C programming, the "!" (exclamation mark) is not a relational operator. Instead, it is used as a logical NOT operator to reverse the truth value of an expression.

What are logical operators in C?

Logical operators in C are symbols used to manipulate Boolean values. They include && (logical AND), || (logical OR), and ! (logical NOT).

Can relational operators be used to compare two strings in C?

Though relational operators can be used to compare individual characters of a string based on their ASCII value, they can’t be used to compare two strings in C.

Can relational operators be used to compare any data types in C?

Yes, they can be used with any data type (int, float, char, etc) that supports comparison.

How do relational operators work in C?

Relational Operators in C work by comparing two operands and return a Boolean value (either true or false) based on the given condition.

Is “=!” A relational operator in C?

No, “=!” Is not a valid relational operator in C or in any other programming language. The correct representation is “!=”.

Conclusion

In this article, we learned about different types of relational operators in C. We got to know that relational operators are an important part of programming. Further, we understood how these operators work and how to use them in code. We learned to write effective programs that perform the necessary operations and make logical decisions based on given conditions.

To learn more about Relational Operators, we recommend reading the following articles: