“From today on, study hard and make progress every day”
Repetition is the best method for memory; spend one minute each day to remember the basics of C language.
“C Language Beginner’s Essential Knowledge Notes Series of 100 Articles”“
19. Relational Operators – Comparing Values is Simple, but Beware of These Errors!
1. Basic Concept of Relational Operators
Relational operators are used to compare the size relationship between two values, returning a boolean value (0 for false, 1 for true):
| Operator | Name | Example | Function Description |
| > | Greater than | a > b | Is a greater than b |
| < | Less than | a < b | Is a less than b |
| >= | Greater than or equal to | a >= b | Is a greater than or equal to b |
| <= | Less than or equal to | a <= b | Is a less than or equal to b |
| == | Equal to | a == b | Is a equal to b |
| != | Not equal to | a != b | Is a not equal to b |
2. Basic Usage Examples
1. Numerical Comparison
int a = 10, b = 20;
printf("%d > %d: %d\n", a, b, a > b); // 0 (false)
printf("%d == %d: %d\n", a, a, a == a); // 1 (true)
2. Character Comparison (Comparing ASCII Codes)
char c1 = 'A', c2 = 'B';
printf("%c < %c: %d\n", c1, c2, c1 < c2); // 1 (65 < 66)
3. Floating Point Comparison
1. Precision Issues
float x = 0.1 + 0.2;
if (x == 0.3) { // may be false
printf("Exactly equal\n");
}
2. Correct Comparison Method
#include <math.h>
#define EPSILON 1e-6
if (fabs(x - 0.3) < EPSILON) {
printf("Approximately equal\n");
}
4. Operator Precedence
The precedence of relational operators:
- 1. Arithmetic operators (+ – * / %) have the highest precedence
- 2. Relational operators (> < >= <=) are next
- 3. Equality operators (== !=) have the lowest precedence
Example:
int result = 5 + 3 < 10 - 2; // equivalent to (5+3) < (10-2)
5. Compound Condition Judgments
1. Range Check
int score = 85;
if (score >= 60 && score <= 100) {
printf("Passed\n");
}
2. Multiple Condition Combinations
char grade = 'B';
if (grade == 'A' || grade == 'B') {
printf("Excellent\n");
}
6. Common Errors
1. Misusing the Assignment Operator
if (a = 5) { // Error! Assigning 5 to a, always true
// ...
}
Correct usage:
if (a == 5) { // Correct comparison
// ...
}
2. Direct Comparison of Floating Point Numbers
float f = 1.0 / 3.0;
if (f == 0.333333) { // Dangerous!
// ...
}
Correct usage:
if (fabs(f - 0.333333) < 1e-5) {
// ...
}
3. Short-Circuit Evaluation
if (a > 0 && b/a > 1) { // b/a will not be calculated if a>0 is false
// ...
}
Some students have contacted me, wanting to have a study exchange group. Previously, I was worried about advertisements, so I didn’t create a group. Considering that having a group is indeed convenient, I will try to create one this time.
If you need it, hurry up and add me, valid for 7 days.

———- End ———-
[Special Statement: All articles in this public account are original or authorized by the author, some content and images are sourced from the internet and AI, please feel free to consume, opinions are for learning reference only~~]


“If you like C, please like it”
Click the bottom right corner to view
“