Comparison operator

Explains the comparison operator in C language. The C comparison operator compares numbers on the left and right sides.

List of comparison operators

A list of comparison operators.

Operator Explanation
== Left side and right side are equal
! = Left and right sides are not equal
& gt; The left side is larger than the right side
& gt; = The left side is greater than or equal to the right side
& lt; The left side is smaller than the right side
& lt; = The left side is less than or equal to the right side

The comparison operator returns an int type "1" if the condition is met, or "0" if the condition is not met. This is a C language specification and is the same for all implementations.

Compare the magnitude of numbers

Let's compare the magnitude of the numbers.

Comparison of magnitude of int32_t type numbers

Let's compare the magnitude of the numbers using the comparison operator. int32_t This is a sample to compare types.

#include <stdint.h>
#include <stdio.h>

int main (void) {
  int32_t num1 = 15;
  int32_t num2 = 10;
  
  if (num1> num2) {
    printf("Match\n");
  }
  else {
    printf("Not Match\n");
  }
}

Comparison of magnitude of int64_t type numbers

This is a sample to compare int64_t types with the comparison operator.

#include <stdint.h>
#include <stdio.h>

int main (void) {
  int64_t num1 = 15;
  int64_t num2 = 10;
  
  if (num1> num2) {
    printf("Match\n");
  }
  else {
    printf("Not Match\n");
  }
}

Comparison of magnitude between int32_t type and int8_t type numbers

This is a sample to compare int32_t type and int8_t type with the comparison operator. ..

#include <stdint.h>
#include <stdio.h>

int main (void) {
  int32_t num1 = 15;
  int8_t num2 = 10;
  
  if (num1> num2) {
    printf("Match\n");
  }
  else {
    printf("Not Match\n");
  }
}

In C, there is a rule that both sides of a binary operator are extended to an int if the type is smaller than the int. Also, if the types on both sides are of different sizes, the smaller type values ​​are converted to the larger type values.

Assuming that the implementation int size is 16bit, let's rewrite the above description using an explicit type cast.

First, int8_t (signed 8bit integer) is converted to int(signed 16bit integer), and then to int32_t (signed 32bit integer).

#include <stdint.h>
#include <stdio.h>

int main (void) {
  int32_t num1 = 15;
  int8_t num2 = 10;
  
  // If you explicitly write a type cast
  if (num1> (int32_t) (int) num2) {
    printf("Match\n");
  }
  else {
    printf("Not Match\n");
  }
}

The implicit conversion rules are the same for other types. Keep in mind that the size of the type is in order, "double" "float" "int64_t" "int32_t" "int16_t" "int8_t".

Comparison of magnitude of uint32_t type numbers

Let's compare the magnitude of the numbers using the comparison operator. uint32_t This is a sample to compare types.

#include <stdint.h>
#include <stdio.h>

int main (void) {
  uint32_t num1 = 15;
  uint32_t num2 = 10;
  
  if (num1> num2) {
    printf("Match\n");
  }
  else {
    printf("Not Match\n");
  }
}

Reasons why we do not recommend comparing signed and unsigned integers

Comparing unsigned and signed integers is not recommended. It's a good idea to explicitly typecast and compare, as unintended implicit conversions may occur.

Comparison between float types

This is a sample to compare float type with the comparison operator.

#include <stdint.h>
#include <stdio.h>

int main (void) {
  float num1 = 15.5f;
  float num2 = 10.2f;
  
  if (num1> num2) {
    printf("Match\n");
  }
  else {
    printf("Not Match\n");
  }
}

Comparison between double types

This is a sample to compare double type with the comparison operator.

#include <stdint.h>
#include <stdio.h>

int main (void) {
  double num1 = 15.5;
  double num2 = 10.2;
  
  if (num1> num2) {
    printf("Match\n");
  }
  else {
    printf("Not Match\n");
  }
}

Comparison of pointers

Let's compare pointers. Addresses are assigned to pointers, but addresses are numbers. The C language comparison operator compares numbers, but you can also use the comparison operator to determine if the addresses are the same.

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

int main (void) {
  // Pointer
  int32_t * nums1 = calloc (sizeof (int32_t), 10);
  int32_t * nums2 = nums1;
  
  if (nums1 == nums2) {
    printf("Match\n");
  }
  else {
    printf("Not Match\n");
  }
  free (nums1);
}

Pointer to NULL comparison

Let's compare the pointer with NULL. NULL is equivalent to 0 and can be compared with the comparison operator.

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

int main (void) {
  // Pointer
  int32_t * nums1 = calloc (sizeof (int32_t), 10);
  
  // Pointer is not null
  if (nums1! = NULL) {
    printf("Match\n");
  }
  else {
    printf("Not Match\n");
  }
  free (nums1);
}

Associated Information