C Comparison Operators

In this lesson, you will learn about Comparison Operations in C, their usage, and examples to better understand the topic.

What are Comparison Operators in C?

Comparison operators are used to performing common comparison operations between numeric values. The table below illustrates the common comparison operations in C:

Arithmetic Operators SymbolArithmetic Operators NameExample
=Equala = b
<>Not equala <> b
! =Not equala != b
= = =Identicala === b
! ==Not identicala !== b
>Greater thana > b
>=Greater than equal toa >= b
<Less thana < b
<=Less than equal toa <= b
<=>Spaceshipa <=> b

Example of Comparision Operator in C

// 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.