Summary: in this tutorial, you’ll learn about the C relational operators and how to use the relational operator to compare two values.
Introduction to the C relational operators
In programming, relational operators are important for making decisions. They allow you to compare two values and check if one is greater than, less than, and equal to another. The relational operators are also known as comparison operators.
The following table illustrates the relation operators in C:
Relational Operators | Description |
---|---|
== | Equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
!= | Not equal to |
It’s important to note that the equal operator uses two equal signs (==) instead of one(=). If you use just one equal sign (=
), it is an assignment operator, not the equal to operator.
C relational operator example
The following program uses the relational operator less than (<) to compare the counter with the MAX constant:
#include <stdio.h>
#include <stdbool.h>
int main()
{
const int MAX = 10;
int counter = 0;
bool can_increase = counter < MAX;
printf("%d\n", can_increase); // 1 or true
return 0;
}
Code language: C++ (cpp)
Summary
- Use the relational operators to compare two values and return a boolean value.