Relational Operators In C

Operator Meaning Example (a = 5, b = 3) Result
== Equal to a == b 0 (false)
!= Not equal to a != b 1 (true)
> Greater than a > b 1 (true)
< Less than a < b 0 (false)
>= Greater than or equal a >= b 1 (true)
<= Less than or equal a <= b 0 (false)

Types of Relational Operators in C

There are mainly six different types of operators. They are as follows:

  • Equal to (==)
  • Not Equal to (!=)
  • Greater than (>)
  • Less than (<)
  • Greater than or equal to (>=)
  • Less than or equal to (<=)

Here, we will discuss these relational operators one by one.

Equal To Operator (==)

In C programming , the Equal To Operator (==) is used to compare both operands and returns 1 if both are equal or the same, and 0 represents the operands that are not equal. For example, if a equals b, if (a == b), it yields true.

Syntax

It has the following syntax:

Example

Opr1 == Opr2

C Example for Equal To(==) Operator

Let us take an example to illustrate the Equal To (==) Operator in C.

Example

Example

#include <stdio.h>  

#include <math.h>  

int main ()   //main function

{  

    int a = 5;  

    int b = 10;  

      

    // Use Equal To Operator  

    printf (" a == b : %d", (a == b));  

    if (a == b)  

        printf ("\n %d is equal to %d", a, b);  

    else  

        printf (" \n %d is not equal to %d", a, b);  

  

      

    int x = 5;  

    int y = 5;  

      

    // Use Equal To Operator  

    printf (" \n x == y : %d", (x == y));  

    if (a == b)  

        printf (" \n %d is equal to %d", x, y);  

    else  

        printf ("\n %d is not equal to %d", x, y);  

    return 0;     

}

Output:

Output

a == b : 0

5 is not equal to 10

x == y : 1

5 is not equal to 5

Explanation:

In this example, we demonstrate the use of the equal-to (==) relational operator. After that, it compares two pairs of integers (a and b, then x and y) and prints whether they are equal or not. The output is based on the result of the comparison: if the values are equal, it prints that they are equal; otherwise, it states they are not.

Not Equal To (!=) Operator

In C programming, the Not Equal To Operator is the opposite of the Equal To Operator and is represented as the (!=) operator. The Not Equal To Operator compares two operands and returns 1 if both operands are not the same; otherwise, it returns 0. For example, if a is not equal to b, if (a!= b), it returns true.

Syntax

It has the following syntax:

Example

Opr1 != Opr2;

C Example for Not Equal To(!=) Operator

Let us take an example to illustrate the Not Equal To (!=) Operator in C.

Example

Example

#include <stdio.h>  

#include <math.h>  

int main ()   //main function

{  

    int a = 5;  

    int b = 10;  

      

    // Use Not Equal To (!=) Operator  

    printf ("a != b : %d", (a != b));  

    if (a != b)  

        printf ("\n%d is equal to %d", a, b);  

    else  

        printf ("\n%d is not equal to %d", a, b);  

  

      

    int x = 5;  

    int y = 5;  

      

    // Use Not Equal To (!=) Operator  

    printf ("\nx != y : %d", (x != y));  

    if (a != b)  

        printf ("\n%d is equal to %d", x, y);  

    else  

        printf ("\n%d is not equal to %d", x, y);  

    return 0;     

}

Output:

Output

a != b : 1

5 is equal to 10

x != y : 0

5 is equal to 5

Explanation:

In this example, we demonstrate the use of the not equal to (!=) relational operator. After that, it compares two sets of integers (a & b, and x & y) and displays whether they are unequal. According to the comparison, it displays corresponding messages, though the condition inside the second if block incorrectly compares a != b instead of x != y.

Greater than(>) Operator

In C programming, the operator checks whether the value of the left operand is greater than the right operand, and if the statement is true, the operator is said to be the Greater Than Operator. For example, if (a > b) is true, a is greater than b.

C Example for Greater than(>) Operator

Let us take an example to illustrate the Greater than (>) Operator in C.

Example

Example

#include <stdio.h>  

  

int main ()    //main function

{  

    int num1, num2;  

    printf (" Enter the value of num1: ");  

    scanf (" %d", &num1);  

      

    printf (" \n Enter the value of num2: ");  

    scanf (" %d", &num2);  

      

    // use greater than operator (>)   

    if (num1 > num2)  

    {  

        printf (" \n The value of num1 is greater than num2.");  

    }  

    else  

    {  

        printf (" \n The value of num2 is greater than num1.");  

    }  

    return 0;  

}

Output:

Output

Enter the value of num1: 20



Enter the value of num2: 30



The value of num2 is greater than num1.

Explanation:

In this example, we demonstrate the greater than (>) relational operator. Here, we take two integer inputs from the user and compare them. After that, it shows whether num1 is greater than num2 or not.

Less than (<) Operator

In C programming, the less than operator is used to check that the left operand is smaller than the right operation. If the condition is true, it returns 1. If the condition is not matched, it returns 0. An example would be if (a < b) - true if a is less than b.

C Example for Less than(<) Operator

Let us take an example to illustrate the Less than (<) Operator in C.

Example

Example

#include <stdio.h>  

  

int main ()    //main function

{  

    int num1, num2;  

    printf (" Enter the value of num1: ");  

    scanf (" %d", &num1);  

      

    printf (" \n Enter the value of num2: ");  

    scanf (" %d", &num2);  

      

    // use less than operator (<)   

    if (num1 < num2)  

    {  

        printf (" \n The value of num1 is less than num2.");  

    }  

    else  

    {  

        printf (" \n The value of num2 is less than num1.");  

    }  

    return 0;  

}

Output:

Output

Enter the value of num1: 45

Enter the value of num2

The value of num2 is greater than num1.

Explanation:

In this example, we demonstrate the less than (<) relational operator. Here, we take two integer inputs from the user and compare them to determine which one is smaller. After that, the program displays whether num1 is less than num2 or not.

Greater than or Equal To (>=) Operator

In C programming, the operator checks whether the left operand's value is greater than or equal to the right operand. If the statement is true, the operator is said to be the Greater than or Equal to Operator. For example, if (a >= b) - true if a is bigger or equal to b.

C Example for Greater Than or Equal To (>=) Operator

Let us take an example to illustrate the Greater than or Equal To (>=) Operator in C.

Example

Example

#include <stdio.h>  

  

int main ()   //main function

{  

    int num1, num2;  

    printf ("Enter the value of num1: ");  

    scanf ("%d", &num1);  

      

    printf ("\nEnter the value of num2: ");  

    scanf ("%d", &num2);  

      

    // use greater than equal to operator (>=)   

    if (num1 > num2)  

    {  

        printf ("\nThe value of num1 is greater than num2.");  

    }  

    else if (num1 >= num2 )  // greater than operator (>=)  

    {  

        printf ("\nThe value of num1 is equal to num2.");  

    }  

    else   

    {  

        printf ("\nThe value of num2 is greater than num1.");  

    }  

    return 0;  

}

Output:

Output

Enter the value of num1: 28

Enter the value of num2: 79

The value of num2 is greater than num1.

Explanation:

In this example, we demonstrate the use of the greater than or equal to (>=) relational operator. Here, we compare two user-input integers and check if num1 is greater than, equal to, or less than num2.

Less than or Equal To (<=) Operator

In C programming, the operator checks whether the value of the left operand is less than or equal to the right operand, and if the statement is true, the operator is said to be the Less than or Equal To Operator.

C Example for Less than or Equal To(<=) Operator

Let us take an example to illustrate the Less than or Equal To (<=) Operator in C.

Example

Example

#include <stdio.h>  

  

int main ()   //main function

{  

    int num1, num2;  

    printf ("Enter the value of num1: ");  

    scanf ("%d", &num1);  

      

    printf ("\nEnter the value of num2: ");  

    scanf ("%d", &num2);  

      

    // use less than equal to operator (<=)   

    if (num1 < num2)  

    {  

        printf ("\nThe value of num1 is less than num2.");  

    }  

    else if (num1 <= num2 )  

    {  

        printf ("\nThe value of num1 is equal to num2.");  

    }  

    else   

    {  

        printf ("\nThe value of num2 is less than num1.");  

    }  

    return 0;  

}

Output:

Output

Enter the value of num1: 45

Enter the value of num2: 45

The value of num1 is equal to num2.

Explanation:

In this example, we demonstrate the less than or equal to (<=) relational operator. Here, we compare two user-provided integers and check if num1 is less than, equal to, or greater than num2.

Advantages of Relational Operators in C

Several advantages of the Relational Operators in C are as follows:

  • In C programming, relational operators allow for consideration of conditions and the use of various decision structures like if, else, and loops.
  • Variable and expression comparisons improve logical control and program functionality.
  • They are easy to perform and comprehend for both beginners and experts because of their usage of basic mathematical symbols, which makes them intuitive.
  • Disadvantages of Relational Operators in C

Several disadvantages of the Relational Operators in C are as follows:

  • Just Two Operands: Only two values can be compared simultaneously by any relational operator. It is necessary to incorporate several relational and logical operators in complex situations.
  • The equality operator (==) is commonly confused with the assignment operator (=) by novice programmers, which leads to logical issues in programming.
  • May lead to nonsensical reasoning: Misuse of relational operators, such as inverting the operands or using the wrong conditions, can result in unexpected behavior or incorrect program execution.
  • Conclusion

In Conclusion, relational operators in C are among the basic tools that serve comparison of values in C and decisions within its program. They return a Boolean value based on the magnitude, equality, or inequality between two operands. If, while, and for loops in control structures use these operators extensively to manage their flow of execution depending on some condition.

Relational operators make logical decisions by programmers possible, thus making a program dynamic and interactive. The proper understanding and application of these will go a long way to help one write logical and efficient code in C.

Relational Operators FAQ's

1) What is the purpose of relational operators in C?

In C programming, relational operators are used to determine how two values or sentences relate to one another. They are commonly employed in decision-making and loops, and their Boolean output is 0 for false and 1 for true.

2) How are == and = different in C?

In actuality, even though it gives a variable a value, the relational operator == checks two values to ensure equality.

3) Can relational operators be used with a variety of data types?

In C programming, relational operations allow data types like int, float, and char to be compared. However, implicit type conversion may occur during comparison.

4) What is the return value of a relational expression in C?

Relational expressions in C return 1 (true) if the condition is met; otherwise, they return 0 (false).

5) Can conditional statements and loops make use of relational operators?

Yes. Relational operators are commonly used by the if, else, while, for, and do-while statements to control the flow of a program depending on comparisons.

Input Required

This code uses input(). Please provide values below: