Relational operators in C are used to compare values and return a result as true (1) or false (0).
These operators allow you to compare variables and expressions, making them essential for decision-making in conditions, loops, and logical expressions.
1. Relational Operators in C
Here are the main relational operators in C:
Operator | Symbol | Description | Example |
---|---|---|---|
Equal to | == | Returns true if both operands are equal | a == b |
Not equal to | != | Returns true if operands are not equal | a != b |
Greater than | > | Returns true if the first operand is greater than the second | a > b |
Less than | < | Returns true if the first operand is less than the second | a < b |
Greater than or equal to | >= | Returns true if the first operand is greater than or equal to the second | a >= b |
Less than or equal to | <= | Returns true if the first operand is less than or equal to the second | a <= b |
2. Example: Basic Comparison of Two Variables
In this example, we use each relational operator to compare two integer variables and display the results.
#include <stdio.h> int main() { int a = 10, b = 20; printf("a = %d, b = %d\n", a, b); printf("a == b: %d\n", a == b); // Equal to printf("a != b: %d\n", a != b); // Not equal to printf("a > b: %d\n", a > b); // Greater than printf("a < b: %d\n", a < b); // Less than printf("a >= b: %d\n", a >= b); // Greater than or equal to printf("a <= b: %d\n", a <= b); // Less than or equal to return 0; }
Output:
a = 10, b = 20 a == b: 0 a != b: 1 a > b: 0 a < b: 1 a >= b: 0 a <= b: 1
Explanation:
- The relational expressions evaluate to either 1 (true) or 0 (false).
- a == b is false (0) because 10 is not equal to 20.
- a < b is true (1) because 10 is less than 20.
3. Using Relational Operators in if Statements
Relational operators are frequently used in if statements to make decisions based on comparisons.
#include <stdio.h> int main() { int age = 18; if (age >= 18) { printf("You are eligible to vote.\n"); } else { printf("You are not eligible to vote.\n"); } return 0; }
In this example:
- age >= 18 checks if age is at least 18.
- If the condition is true, it prints “You are eligible to vote.” Otherwise, it prints “You are not eligible to vote.”
Output (assuming age is 18):
You are eligible to vote.
4. Using Relational Operators in while Loops
Relational operators can be used in loops to control how many times the loop will execute.
#include <stdio.h> int main() { int count = 1; printf("Counting from 1 to 5:\n"); while (count <= 5) { printf("%d\n", count); count++; } return 0; }
In this example:
- count <= 5 is the condition for the while loop.
- The loop continues as long as count is less than or equal to 5.
Output:
Counting from 1 to 5: 1 2 3 4 5
5. Using Relational Operators in Complex Conditions
You can combine multiple relational expressions in complex conditions using logical operators like && (and) and || (or).
#include <stdio.h> int main() { int age = 25; float income = 40000; if (age >= 18 && income >= 30000) { printf("You are eligible for the loan.\n"); } else { printf("You are not eligible for the loan.\n"); } return 0; }
In this example:
- The condition age >= 18 && income >= 30000 checks if both conditions are true.
- If age is 18 or older and income is at least 30000, it prints “You are eligible for the loan.”
Output (assuming age is 25 and income is 40000):
You are eligible for the loan.
6. Comparing Floating-Point Numbers
When comparing floating-point numbers, precision issues can arise, so it’s best to use a small tolerance value instead of direct comparison.
#include <stdio.h> #include <math.h> int main() { float x = 0.1 * 10; float y = 1.0; float epsilon = 0.00001; // Small tolerance for comparison if (fabs(x - y) < epsilon) { printf("x and y are approximately equal.\n"); } else { printf("x and y are not equal.\n"); } return 0; }
In this example:
- fabs(x – y) < epsilon checks if the difference between x and y is less than a small tolerance (epsilon), indicating they are approximately equal.
- This method avoids issues due to the limited precision of floating-point numbers.
Output:
x and y are approximately equal.
7. Using Relational Operators in Arrays and Loops
Relational operators are useful when working with arrays, such as finding the maximum or minimum value.
Example: Finding the Maximum Value in an Array
#include <stdio.h> int main() { int numbers[] = {3, 15, 7, 9, 20, 12}; int max = numbers[0]; for (int i = 1; i < 6; i++) { if (numbers[i] > max) { max = numbers[i]; } } printf("The maximum value is: %d\n", max); return 0; }
In this example:
- if (numbers[i] > max) checks if each element is greater than the current max.
- If true, max is updated to that element.
Output:
The maximum value is: 20
8. Comparing Characters with Relational Operators
In C, characters are represented by their ASCII values, so you can use relational operators to compare characters.
#include <stdio.h> int main() { char char1 = 'A'; char char2 = 'B'; if (char1 < char2) { printf("'%c' comes before '%c' in ASCII.\n", char1, char2); } else { printf("'%c' comes after '%c' in ASCII.\n", char1, char2); } return 0; }
In this example:
- The ASCII value of ‘A' (65) is less than that of ‘B' (66).
- char1 < char2 evaluates as true.
Output:
'A' comes before 'B' in ASCII.
9. Summary Table of Relational Operators
Operator | Description | Example | Result (assuming a = 10 and b = 20) |
---|---|---|---|
== | Equal to | a == b | 0 (false) |
!= | Not equal to | a != b | 1 (true) |
> | Greater than | a > b | 0 (false) |
< | Less than | a < b | 1 (true) |
>= | Greater than or equal to | a >= b | 0 (false) |
<= | Less than or equal to | a <= b | 1 (true) |
10. Complete Program Example: Comparing Values
This program takes two integer inputs and uses relational operators to compare them, displaying the results.
#include <stdio.h> int main() { int num1, num2; printf("Enter first number: "); scanf("%d", &num1); printf("Enter second number: "); scanf("%d", &num2); printf("\nComparison Results:\n"); printf("%d == %d: %d\n", num1, num2, num 1 == num2); printf("%d != %d: %d\n", num1, num2, num1 != num2); printf("%d > %d: %d\n", num1, num2, num1 > num2); printf("%d < %d: %d\n", num1, num2, num1 < num2); printf("%d >= %d: %d\n", num1, num2, num1 >= num2); printf("%d <= %d: %d\n", num1, num2, num1 <= num2); return 0; }
Example Input/Output:
For inputs 10 and 20:
Enter first number: 10 Enter second number: 20 Comparison Results: 10 == 20: 0 10 != 20: 1 10 > 20: 0 10 < 20: 1 10 >= 20: 0 10 <= 20: 1
This tutorial covers relational operators in C, showing how to use them in various contexts such as conditions, loops, arrays, and character comparisons.