Relational Operators in C | About and Types

Relational Operators in C | About and Types

6 mins read142 Views Comment
Esha
Esha Gupta
Associate Senior Executive
Updated on Apr 25, 2024 14:05 IST

Relational operators in C, such as “less than”, “greater than”, “less than or equal to”, “greater than or equal to”, “equal to”, and “not equal to”, play a pivotal role in comparing values. They form the backbone of decision-making structures, guiding the program’s flow based on these comparisons. Let's understand more!

2023_08_MicrosoftTeams-image-3.jpg

In C programming, decisions are based on comparing values. Relational operators are essential tools that enable these comparisons, guiding the flow and logic of our applications. In this blog, we’ll explore the specifics of these operators and their crucial role in C.

Explore Online C Programming Courses

What are relational operators in C?

Relational operators in C are used to compare two values, determining their relationship. Based on the comparison, these operators return a boolean value: 1 (true) if the condition is satisfied and 0 (false) otherwise.

Here is a list of all the relational operators we have in C:

Let’s understand each of these in detail :

1. Equal to (==)

Checks if two values are the same.
Let’s see a scenario: Imagine a security system that checks if the entered PIN matches the stored PIN.


 
#include <stdio.h>
int main() {
int enteredPIN = 1930, storedPIN = 1930;
if (enteredPIN == storedPIN) {
printf("Access granted.\n");
}
return 0;
}
Copy code

Output :

Access granted.

2. Not equal to (!=)

Verifies if two values are different.

Let’s see a Scenario: Consider a digital library system where users can borrow e-books. Each e-book has a unique ID. When a user wants to return an e-book, the system needs to ensure the returned e-book ID doesn’t match any of the currently blacklisted e-book IDs (e.g., due to copyright issues or inappropriate content).


 
#include <stdio.h>
int main() {
int returnedEbookID = 1930;
int blacklistedEbookID = 1906;
if (returnedEbookID != blacklistedEbookID) {
printf("E-book return accepted.\n");
} else {
printf("This e-book ID is blacklisted. Return not accepted.\n");
}
return 0;
}
Copy code

Output :

E-book return accepted.
Operators in C programming: A Complete Tutorial
C programming Keywords: A list and Explanation of Usage
Control Statements in C | Meaning and Types

3. Less than (<)

Determines if the left value is smaller than the right.

Let’s see a scenario: Imagine a smart refrigerator that checks if the weight of milk in a container is less than a minimum level. If it is, the refrigerator sends a reminder to buy more milk.


 
#include <stdio.h>
int main() {
double currentMilkWeight = 0.7; // in kilograms
double minMilkWeight = 2.0; // set minimum level
if (currentMilkWeight < minMilkWeight) {
printf("Reminder: Time to buy more milk!\n");
}
return 0;
}
Copy code

Output :

Reminder: Time to buy more milk!
 

4. Greater than (>)

Confirms if the left value is larger than the right.

Let’s see a Scenario: A fitness application measures the number of steps a user takes daily. If steps taken surpass a certain goal, the user gets a congratulatory notification.


 
#include <stdio.h>
int main() {
int customerAge = 30;
int minimumRentalAge = 19;
if (customerAge >= minimumRentalAge) {
printf("You are eligible to rent a car from us.\n");
}
return 0;
}
Copy code

Output :

You are eligible to rent a car from us.

5. Less than or equal to (<=)

Verifies if the left value is smaller than or equal to the right.

Let’s see a Scenario: In an online learning platform, if a student’s total score in a course is less than or equal to 50%, the system marks them as needing extra help and suggests supplementary materials.


 
#include <stdio.h>
int main() {
double studentScore = 18.5; // as a percentage
double threshold = 50.0;
if (studentScore <= threshold) {
printf("Suggestion: Consider reviewing supplementary materials for better understanding.\n");
}
return 0;
}
Copy code

Output :

Suggestion: Consider reviewing supplementary materials for better understanding.

6. Greater than or equal to (>=)

Checks if the left value is larger than or equal to the right.

Let’s see a Scenario: A car rental service has a minimum age requirement. If the customer’s age is greater than or equal to the required age, they’re eligible to rent a car.


 
#include <stdio.h>
int main() {
int customerAge = 27;
int minimumRentalAge = 26;
if (customerAge >= minimumRentalAge) {
printf("You are eligible to rent a car from us.\n");
}
return 0;
}
Copy code

Output :

You are eligible to rent a car from us.
Recommended online courses

Best-suited C / C++ courses for you

Learn C / C++ with these high-rated online courses

4 K
2 months
– / –
6 months
– / –
1 month
3.5 K
3 months
15 K
2 months
– / –
2 months
– / –
50 hours
– / –
1 month
5.5 K
140 hours
– / –
4 months

Conclusion

Relational operators in C are fundamental building blocks that enable programs to make decisions based on the comparison of values. They are the foundation for constructing conditions in statements like if, while, and for. By comparing variables, constants, or expressions, relational operators help determine the flow of a program, allowing developers to handle many scenarios. From simple checks, like determining if a user has met a score threshold, to more complex conditions in advanced algorithms, these operators are indispensable. As with many aspects of C, mastering relational operators is essential for creating efficient and effective code.

FAQs

What are Relational Operators in C?

Relational operators in C are used to compare two values or expressions. These operators evaluate the relationship between their operands and return a boolean value, either true (non-zero) or false (zero), based on whether the comparison is true or false. They are fundamental in controlling the flow of a program, particularly in conditional statements and loops.

What are the different types of Relational Operators in C?

C supports several relational operators:

  • == (Equal to): Checks if the values of two operands are equal or not. If yes, then the condition becomes 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.
  • > (Greater than): Checks if the left operand is greater than the right operand. If yes, then the condition becomes true.
  • < (Less than): Checks if the left operand is less than the right operand. If yes, then the condition becomes true.
  • >= (Greater than or Equal to): Checks if the left operand is greater than or equal to the right operand. If yes, then the condition becomes true.
  • <= (Less than or Equal to): Checks if the left operand is less than or equal to the right operand. If yes, then the condition becomes true.

How are Relational Operators used in Conditional Statements?

Relational operators are often used in if, else if, and else statements to perform different actions based on different conditions. For example, in an if statement, the condition might check if one operand is greater than another (if (a > b)), and execute a block of code accordingly.

Can Relational Operators be used with Different Data Types?

Yes, relational operators can be used with various data types such as integers, floats, and characters. However, when comparing different data types, like an integer with a float, type conversion rules are applied. It's essential to understand type promotion rules in C to avoid unexpected results.

Are Relational Operators used in Loop Conditions?

Yes, relational operators play a critical role in loop conditions like for, while, and do-while loops. They are used to set the condition for the continuation of the loop. For example, in a for loop (for (i = 0; i < 10; i++)), the relational operator < is used to determine when the loop should end.

What is the result type of expressions involving relational operators in C?

In C, any expression that involves relational operators (<, >, <=, >=, ==, !=) evaluates to an integer type. Specifically, the result is of type int and the values returned are either 1 (true) or 0 (false).

Can relational operators be used in complex expressions, and how is precedence handled?

Yes, relational operators can be part of complex expressions involving both logical and arithmetic operators. In terms of precedence, relational operators (<, >, <=, >=) have higher precedence than equality operators (==, !=), which in turn have higher precedence than logical operators (&&, ||). However, arithmetic operators (+, -, *, /, %) have higher precedence than relational operators.

What happens when relational operators are used with pointers in C?

Relational operators can be used with pointers to compare the addresses they hold. This is useful in scenarios such as sorting arrays of pointers or verifying the range of an allocated block of memory. The operators compare the memory addresses stored in the pointers, not the content at those addresses.

Is there a performance difference when using different relational operators?

In general, there is no noticeable performance difference among the relational operators (<, >, <=, >=, ==, !=) because they all are simple comparisons that typically compile to a single machine instruction. However, the efficiency can depend on the architecture and compiler optimization techniques.

How do relational operators interact with unsigned vs. signed integers?

When relational operators are used with mixed signed and unsigned integers, the usual arithmetic conversions are applied. This means that if one operand is signed and the other is unsigned, the signed operand is typically converted to unsigned, which can lead to unexpected results if the signed value is negative. Therefore, caution is advised when comparing signed and unsigned values using relational operators to avoid potential bugs due to value wrapping.

About the Author
author-image
Esha Gupta
Associate Senior Executive

Hello, world! I'm Esha Gupta, your go-to Technical Content Developer focusing on Java, Data Structures and Algorithms, and Front End Development. Alongside these specialities, I have a zest for immersing myself in v... Read Full Bio