In this lesson, you will learn about Comparison Operations in C, their usage, and examples to better understand the topic.
Comparison operators are used to performing common comparison operations between numeric values. The table below illustrates the common comparison operations in C:
Arithmetic Operators Symbol | Arithmetic Operators Name | Example |
---|---|---|
= | Equal | a = b |
<> | Not equal | a <> b |
! = | Not equal | a != b |
= = = | Identical | a === b |
! == | Not identical | a !== b |
> | Greater than | a > b |
>= | Greater than equal to | a >= b |
< | Less than | a < b |
<= | Less than equal to | a <= b |
<=> | Spaceship | a <=> b |
// Example Of Comparison Operator in C #include <stdio.h> int a, b, c; int main() { a = 24; b = 12; printf("a = 24"); printf("\n"); printf("b = 12"); printf("\n"); // = if (a == b) { printf("%d and %d operands are Equal ", a, b); } else { printf("%d and %d operands are not Equal ", a, b); } printf("\n"); // > if (a > b) { printf("%d value operand is greater than %d ", a, b); } else { printf("%d value operand is not greater than %d", a, b); } printf("\n"); // < if (a < b) { printf("%d value operand is less than %d", a, b); } else { printf("%d value operand is not less than %d ", a, b); } printf("\n"); // != if (a != b) { printf("%d is not equal to %d ", a, b); } else { printf("%d is equal to %d ", a, b); } printf("\n"); //>= if (a >= b) { printf("%d is either greater than or equal to %d", a, b); } else { printf("%d is neither greater than nor equal to %d", a, b); } printf("\n"); //<= if (a <= b) { printf("%d is either less than or equal to %d", a, b); } else { printf("%d is neither less than nor equal to %d", a, b); } return 0; }
Output
a = 24 b = 12 24 and 12 operands are not Equal 24 value operand is greater than 12 24 value operand is not less than 12 24 is not equal to 12 24 is either greater than or equal to 12 24 is neither less than nor equal to 12
In the next lesson, you will learn another C operator type, the logical operator.