Relational Operators in C

Relational Operators in C

Relational operators in C are used to check the relation between two operands. 0 and 1 are the results given by relational operators. If the relationship comes out to be true, it returns 1. If the relationship is false, it returns 0.

The list of relational operators is given below:

OperatorDescription
==Equal to: To check whether two operands are equal or not.
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

 

Example

Let’s understand all operators through a program.

#include<stdio.h>
int main()
{
            int a=8;
            int b=3;
            printf("%d",a==b);
            printf("\n%d",a!=b);
            printf("\n%d",a>b);
            printf("\n%d",a<b);
            printf("\n%d",a>=b);
            printf("\n%d",a<=b);


}

Output

Relational Operators in C

Explanation

Equality operator returns 0 since 8==3 is false.

Not equal to operator returns 1 since 8!=3 is true.

Greater than operator returns 1 since 8>3 is true.

Less than operator returns 0 since 8<3 is false.

Greater than or equal to operator returns 1 since 8>=3 is true.

Less than or equal to operator returns 0 since 8<=3 is false.

Note: If either of the relations in ‘>=’ and ‘<=’ is true, the result will be 1. In ‘8>=3’, 8 is greater than 3, but 8 equals 3 is false. However, the overall output is true. In ‘8<=3’, both relations are false. Hence, the output is 0.

Difference Between ‘=’ and ‘==’

Most of the users always have confusion in ‘=’ and ‘==’ operators. However, both look the same but work differently in our programming languages.

=’ is the assignment operator that assigns a value to a variable.

==’ is the equality operator that compares the equality of two operands.

For example,

Y=5;

The above statement means we are assigning ‘5’ in the variable ‘Y’. Always remember that assignment is done from right to left.

Difference Between ‘=’ and ‘==’


Y==5;
 

Here, we compare whether ‘5’ is equal to ‘Y’ or not.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.