C Programming Relational Operators

C Programming Relational Operators

Relational operators in c programming; Through this tutorial, you will learn about relational operators in c programming and how to use them in c programs.

Relational Operators In C Programming

A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0.

The below table shows all the Relational Operators in C programming with examples.

OperatorMeaning of OperatorExample
==Equal to7 == 3 is evaluated to 0
>Greater than7 > 3 is evaluated to 1
<Less than7 < 3 is evaluated to 0
!=Not equal to7 != 3 is evaluated to 1
>=Greater than or equal to7 >= 3 is evaluated to 1
<=Less than or equal to7 <= 3 is evaluated to 0

Note that:- Relational operators are used in decision making and loops.

Example 1 – Relational Operators

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

Note that:- Relational operators in C are commonly used to check the relationship between the two variables

Example 2 – C Relational Operators with If else Statement

/* C Relational Operators in If Condition */

#include <stdio.h>

void main()
{
  int x = 10;
  int y = 25;

  if (x == y)
   {
     printf(" x is equal to y \n" );
   }

  else
   {
     printf(" x is not equal to y \n" );
   }

}

Output

 x is not equal to y 

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *