C Comparison Operators
In C, comparison operators are used to compare two values. These operators return a boolean value: true (1) if the comparison is correct, or false (0) otherwise. Comparison operators are commonly used in conditional statements like if and loops.
List of Comparison Operators
| Operator Name | Operator Symbol | Description | 
|---|---|---|
| Equal-To | == | Returns trueif both operands are equal. | 
| Not-Equal | != | Returns trueif both operands are not equal. | 
| Greater-Than | > | Returns trueif the left operand is greater than the right operand. | 
| Less-Than | < | Returns trueif the left operand is less than the right operand. | 
| Greater-Than or Equal-To | >= | Returns trueif the left operand is greater than or equal to the right operand. | 
| Less-Than or Equal-To | <= | Returns trueif the left operand is less than or equal to the right operand. | 
Examples of Comparison Operators
1. Using Equal-To (==) Operator
In this example, we will compare two integers using the Equal-To operator.
main.c
</>
                        Copy
                        #include <stdio.h>
int main() {
    int a = 10, b = 10;
    if (a == b) {
        printf("Both numbers are equal.\n");
    } else {
        printf("Numbers are not equal.\n");
    }
    return 0;
}Output:
Both numbers are equal.2. Using Not-Equal (!=) Operator
In this example, we will compare two integers using the Not-Equal operator.
main.c
</>
                        Copy
                        #include <stdio.h>
int main() {
    int x = 15, y = 20;
    if (x != y) {
        printf("Numbers are not equal.\n");
    } else {
        printf("Numbers are equal.\n");
    }
    return 0;
}Output:
Numbers are not equal.3. Using Greater-Than (>) Operator
In this example, we will check if one number is greater than another.
main.c
</>
                        Copy
                        #include <stdio.h>
int main() {
    int a = 30, b = 20;
    if (a > b) {
        printf("a is greater than b.\n");
    } else {
        printf("a is not greater than b.\n");
    }
    return 0;
}Output:
a is greater than b.4. Using Less-Than (<) Operator
In this example, we will check if a number is less than another.
main.c
</>
                        Copy
                        #include <stdio.h>
int main() {
    int x = 10, y = 25;
    if (x < y) {
        printf("x is less than y.\n");
    } else {
        printf("x is not less than y.\n");
    }
    return 0;
}Output:
x is less than y.Conclusion
In this tutorial, we explored comparison operators in C:
- Comparison operators are used to compare values and return trueorfalse.
- The Not-Equal (!=), Greater-Than (>), and Less-Than (<) operators help control program flow.
- These operators are commonly used in conditional statements and loops.
Understanding comparison operators is essential for writing effective C programs.
