Skip to main content

Relational Operators

Relational operators

Relational operators compare two operands and return a Boolean value based on the relationship between the operands.

Below are the relational operators in C with a brief description:

  • The equality operator ==: Compare two operands and returns 1 (true) if the operands are equal, and 0 (false) otherwise.
  • The inequality operator!=: Compare two operands and returns 1 (true) if the operands are not equal, and 0 (false) otherwise.
  • The greater than operator>: Compare two operands and returns 1 (true) if the first operand is greater than the second, and 0 (false) otherwise.
  • The less than operator<: Compare two operands and returns 1 (true) if the first operand is less than the second, and 0 (false) otherwise.
  • The greater than or equal to operator>=: RCompare two operands and returns 1 (true) if the first operand is greater than or equal to the second, and 0 (false) otherwise.
  • The less than or equal to operator<=: RCompare two operands and returns 1 (true) if the first operand is less than or equal to the second, and 0 (false) otherwise.

Relational operators Examples

Example 1

#include <stdio.h>

int main() {
int x = 5, y = 10, z = 15;

// Equality operator
if (x == y) {
printf("x is equal to y\n");
} else {
printf("x is not equal to y\n");
}

// Inequality operator
if (x != y) {
printf("x is not equal to y\n");
} else {
printf("x is equal to y\n");
}

// Greater than operator
if (x > y) {
printf("x is greater than y\n");
} else {
printf("x is not greater than y\n");
}

// Less than operator
if (x < y) {
printf("x is less than y\n");
} else {
printf("x is not less than y\n");
}

// Greater than or equal to operator
if (x >= y) {
printf("x is greater than or equal to y\n");
} else {
printf("x is not greater than or equal to y\n");
}

// Less than or equal to operator
if (x <= y) {
printf("x is less than or equal to y\n");
} else {
printf("x is not less than or equal to y\n");
}
return 0;
}
Output:

Output:

x is not equal to y
x is not equal to y
x is not greater than y
x is less than y
x is not greater than or equal to y
x is less than or equal to y

Practical use of relational operators

Relational operators compare two operands and return a Boolean value (either true or false) depending on the comparison. These operators are used where you need to make a decision based on the comparison of two values.

Here are a few examples of how relational operators can be used:

Conditional statements

You can use relational operators in if-else statements to control the flow of your program. For example, you can check whether a user's input is greater than or equal to a certain value before performing a specific action.

if (user_input >= 10) {
// perform action
} else {
// do something else
}

Loops

You can use relational operators in while and for loops to control the number of iterations. For example, you can continue iterating while a counter variable is less than a certain value.

int i = 0;
while (i < 10) {
// do something
i++;
}

Data validation

You can use relational operators to validate user input or data from external sources. For example, you can check whether a user's input is within a certain range before storing it in a variable.

if (user_input > 0 && user_input <= 100) {
int valid_input = user_input;
} else {
printf("Invalid input!\n");
}

Searching

You can use relational operators to search for specific values in an array or a list. For example, you can check whether an element in an array is equal to a certain value.

int array[] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
if (array[i] == 3) {
printf("3 found at index %d\n", i);
break;
}
}

Making Decisions

You can use relational operators to make decisions in your program. For example, in a game, you can check if the player's score is greater than the high score, and if so, update the high score.

if (player_score > high_score) {
high_score = player_score;
}
note

These are just a few examples of how relational operators can be used in practical situations. The possibilities are endless and the use of relational operators is up to the programmer's creativity, depending on the problem they are trying to solve.