Relational Operators in C

The relational operators are used to compare two of the available values to understand what relationship the pairs of values share. For example, equal to, greater than, less than, etc. Here is a list of all the relational operators used in the C language:

  • Equal to
  • Not equal to
  • Less than
  • Greater than
  • Less than or equal to
  • Greater than or equal to

Working of Relational Operators in C

Let us take a look at each of these in more detail.

Equal to Operator

The function of the equal to operator (==) is to check if two of the available operands are equal to each other or not. If it is so, then it returns to be true, or else it returns false. For instance, 3==3 will return to be true. On the other hand, 2==3 will return to be false.

Not equal to Operator

The function of the not equal to operator (!=) is to check if two of the available operands are equal to each other or not. If it is not equal, then it returns to be true, or else it returns false. For instance, 3!=3 will return to be false. On the other hand, 3!=4 will return to be true.

Less than Operator

The function of the less than operator (<) is to check if the first available operand is lesser than the second one. If it is so, then it returns to be true, or else it returns false. For instance, 5<4 will return to be false. On the other hand, 6<8 will return to be true.

Greater than Operator

The function of the greater than operator (>) is to check if the first available operand is greater than the second one. If it is so, then it returns to be true, or else it returns false. For instance, 5>4 will return to be true. On the other hand, 7>8 will return to be false.

Less than or equal to Operator

The function of the less than or equal to operator (<=) is to check if the first available operand is equal to or less than the second one. If it is so, then it returns to be true, or else it returns false. For instance, 9<=9 and 6<=9 will return to be true. On the other hand, 9<=8 will return to be false.

Greater than or equal to operator

The function of the greater than or equal to operator (>=) is to check if the first available operand is equal to or greater than the second one. If it is so, then it returns to be true, or else it returns false. For instance, 2>=2 and 4>=2 will return to be true. On the other hand, 3>=4 will return to be false.

//Working of relational operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;

printf("%d == %d is %d \n", a, b, a == b);
printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);

return 0;
}

Output

5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1

C Logical Operators

An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming.

Example:

// Working of logical operators

#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;

result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", result);

result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d \n", result);

result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d \n", result);

result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d \n", result);

result = !(a != b);
printf("!(a != b) is %d \n", result);

result = !(a == b);
printf("!(a == b) is %d \n", result);

return 0;
}

Output

(a == b) && (c > b) is 1 
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0

Explanation of logical operator program

  • (a == b) && (c > 5) evaluates to 1 because both operands (a == b) and (c > b) is 1 (true).
  • (a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).
  • (a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true).
  • (a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c < b) are 0 (false).
  • !(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a != b) is 1 (true).
  • !(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0 (false).

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store