๐ C Relational Operators โ Compare Values in C Programs
๐งฒ Introduction โ What Are Relational Operators in C?
Relational operators in C are used to compare two values or expressions. These comparisons evaluate to either true (1)
or false (0)
and are essential for building control structures such as if
, while
, and for
loops. They form the foundation of decision-making in C programming.
๐ฏ In this guide, youโll learn:
- The list of relational operators in C
- How to compare variables and expressions
- Use of relational operators in conditional statements
- Examples and common comparison logic
๐ List of Relational Operators
Operator | Description | Example (a = 10 , b = 5 ) | Result |
---|---|---|---|
== | Equal to | a == b | 0 (false) |
!= | Not equal to | a != b | 1 (true) |
> | Greater than | a > b | 1 (true) |
< | Less than | a < b | 0 (false) |
>= | Greater than or equal to | a >= b | 1 (true) |
<= | Less than or equal to | a <= b | 0 (false) |
๐งช Using Relational Operators in Code
#include <stdio.h>
int main() {
int a = 10, b = 5;
printf("a == b: %d\n", a == b);
printf("a != b: %d\n", a != b);
printf("a > b: %d\n", a > b);
printf("a < b: %d\n", a < b);
printf("a >= b: %d\n", a >= b);
printf("a <= b: %d\n", a <= b);
return 0;
}
Output:
a == b: 0
a != b: 1
a > b: 1
a < b: 0
a >= b: 1
a <= b: 0
๐ Relational Operators in Conditional Statements
Relational operators are commonly used inside if
, while
, and for
loops to control program flow:
if (score >= 50) {
printf("Pass\n");
} else {
printf("Fail\n");
}
They can also be used with logical operators (&&
, ||
, !
) for compound conditions.
โ ๏ธ Notes on Relational Operators
- The result of any relational operation is either
1
(true) or0
(false). - Always use
==
for comparison, not=
(assignment). - Comparisons are type-safeโcomparing
int
withfloat
will work via implicit conversion.
โ Example:
int x = 5;
float y = 5.0;
printf("%d\n", x == y); // Output: 1 (true)
๐ Summary โ Recap & Next Steps
Relational operators in C are critical for building decision-making structures. They allow comparison of values, variables, or expressions and are typically used in branching and looping constructs.
๐ Key Takeaways:
- Relational operators return
1
for true,0
for false. - Used heavily in
if
,while
,for
, andswitch
logic. - Must not confuse
==
(comparison) with=
(assignment). - Can compare integers, characters, floats, and mixed types.
โ๏ธ Real-World Relevance:
Used in grading systems, login checks, limit checks, authentication, and any application that needs conditional decision-making.
โ Frequently Asked Questions (FAQ)
โ What is the difference between ==
and =
in C?
โ
==
checks for equality.
โ
=
assigns a value.
if (a == b) { ... } // Correct
if (a = b) { ... } // โ Logic error
โ Can I use relational operators with characters?
โ Yes. Characters are internally treated as integers using ASCII values.
if ('A' < 'Z') // true
โ Do relational operators work with floats?
โ
Yes. You can compare float
and double
values.
float a = 3.14;
if (a > 2.0) { ... }
โ Can relational operators be combined?
โ Yes. Combine with logical operators:
if (score >= 50 && score <= 100) { ... }
โ What does !=
mean?
โ
!=
means “not equal to”. It returns true (1
) if the two operands are different.
Share Now :