top

Search

C Tutorial

.

UpGrad

C Tutorial

Relational Operators in C

Relational operators are operators that allow you to compare two entities. The entities can be characters, integers, and more. They always provide the result as either 1 or 0. Here 1 indicates that the comparison is true, and 0 indicates that the comparison is false. It implies that the output of relational operators in any expression is boolean.

The relational operators in C are used to perform logical comparisons in any expression or conditional statement. Their syntax is somewhat identical to that of logical operators in C. Commonly, relational operators control the program’s flow by assessing conditions and making decisions according to the comparison results.

Today, let’s dive deep in to explore the various types of relational operators in C and how they function. 

Types Of Relational Operators In C

The relational operators compare the two input values to evaluate their relationship. For example, you can relate the two values by determining whether they are equal, unequal, less than, greater than, etc. 

The following list shows all the relational operators in C:

  • Less than

  • Greater than

  • Less than or equal to

  • Greater than or equal to

  • Equal to

  • Not equal to 

Working Of Relational Operators In C

  • Equal To

The equal to operator (==) analyses whether two of the operands are equal to each other or not. If they are equal, the output is true, else false. For example, evaluating 2==3 will return true. But evaluating 2==4 will return false. The equal to operators are also known as assignment operators in C.

  • Not Equal To

This operator is represented with the (!=) symbol. It checks if two of the given operands are equal to each other or not. If they are not equal, the output returns true, else it returns false. For example, 2!=2 will give output as false, whereas 2!=4 will return true. 

  •  Less Than

The less than operator (<) assesses whether the first given operand is lesser than the second one. If yes, then the output returns true, else it returns false. For example,  3<2 will return false. But, 3<4 will return true. 

  • Greater Than

The greater than operator (>) checks if the first given operand is greater than the second one. If yes, then it returns true, else it returns false. For example,  3>2 will return true. But 3>4 will return false. 

  • Less Than Or Equal To

This operator is represented with a (<=) sign. It evaluates if the first given operand is equal to or less than the second one. If yes then it outputs true, else it outputs false. For example, 2<=4 will result as true, but 4<=2 will result as false. 

  • Greater Than Or Equal To

The greater than or equal to operator (>=) checks if the first given operand is equal to or greater than the second one. If yes then it results as true, otherwise, it returns false. For example, 3>=3 and 3>=2 will return true. But 2>=3 will return false.

Example Of Relational Operators In C

Let us look at one of the easy-to-understand relational operators examples that demonstrates how these operators work in C:

#include <stdio.h>
 
int main() 
{
int number1, number2;

    printf("Please enter the first number: ");
    scanf("%d", &number1);
 
    printf("Please enter the second number: ");
    scanf("%d", &number2);
 
    printf("\nHere are the outputs of various relational operators implemented on the given numbers:\n");
 
    printf("%d > %d  is %d\n", number1, number2, number1 < number2);
    printf("%d < %d  is %d\n", number1, number2, number1 > number2);
    printf("%d >= %d is %d\n", number1, number2, number1 <= number2);
    printf("%d <= %d is %d\n", number1, number2, number1 >= number2);
    printf("%d == %d is %d\n", number1, number2, number1 == number2);
    printf("%d != %d is %d\n", number1, number2, number1 != number2);
 
return 0;
}

The above program will print the results of the relational operations we used as per the user's input. For instance, if the user enters 12 as the first number and 15 as the second number, then you will get the following output.

Output:

Please enter the first number: 12
Please enter the second number: 15
Here are the outputs of various relational operators implemented on the given numbers:
12 > 15  is 1
12 < 15  is 0
12 >= 15 is 1
12 <= 15 is 0
12 == 15 is 0
12 != 15 is 1

In the above program, the user is allowed to enter two numbers. The program uses different relational operators to compare these numbers. Subsequently, the program prints the suitable message for each relational operation. The code format is almost the same if you want to implement the relational operators in C#.

The following section explains how each of these operators works:

> (Greater than operator):

It returns 1 when the operand on the left is greater than the one present on the right; else, it returns 0.

< (Less than operator):

This operator returns 1 if the operand at left is less than the right one; else, it returns 0. 

>= (Greater than or equal to operator):

It outputs 1 if the left operand is greater than or equal to the right one; else, it outputs 0. 

<= (Less than or equal to operator):

It outputs 1 if the left operand is less than or equal to the right one; else, it outputs 0.

 == (Equal to operator):

It outputs 1 if both the left and right operands are equal; else, it returns 0.

 != (Not equal to operator):

It returns 1 if the left operand and right operand are unequal; else, it returns 0.

Implementation of Relational Operators in C

The implementation of relational operators in C involves two tasks. They are -comparing values and producing a result (either 0 (false) or 1 (true)) based on the comparison. Here’s an example program that implements relational operators in C:

#include <stdio.h>
int main() 
{
int number1, number2;

printf("Please enter two numbers: ");
scanf("%d %d", &number1, &number2);

// Relational operators
if (number1 == number2) {
printf("%d is equal to %d\n", number1, number2);
}
if (number1 != number2) {
printf("%d is not equal to %d\n", number1, number2);
}
if (number1 > number2) {
printf("%d is greater than %d\n", number1, number2);
}
if (number1 < number2) {
printf("%d is less than %d\n", number1, number2);
}
if (number1 >= number2) {
printf("%d is greater than or equal to %d\n", number1, number2);
}
if (number1 <= number2) {
printf("%d is less than or equal to %d\n", number1, number2);
}
return 0;
}

Output:

Please enter two numbers: 5 7
5 is not equal to 7
5 is less than 7
5 is less than or equal to 7

The above code prompts the user to enter two numbers, i.e., number1 and number2. We then implement different relational operators to compare their values.

Here’s a brief on the relational operators used in the above code:

== : Checks if number1 is equal to number2.

!= : Checks if number1 is not equal to number2.

< : Checks if number1 is less than number2.

> : Checks if number1 is greater than number2.

<= : Checks if number1 is less than or equal to number2.

>= : Checks if number1 is greater than or equal to number2.

Practice Problems On Relational Operators In C

Problem- 1:

#include <stdio.h>
int main() 
{
 
  int x, y;
  x = 7;
  y = 7;
 
  if (x < y) // Less than operator is used to check if A is strictly less than B or not
  {
    printf("x is strictly less than y");
  } else // if the above condition returns false, then the program executes the below block
  {
    printf("x is greater than or equal to y");
  }
}

Output

x is greater than or equal to y

In the above code, the first step initialises the variables x and y. The program then checks whether x<y or not and prints the output message accordingly. 

Problem- 2:

#include <stdio.h>
int main()
{ 
  int x, y;
  x = 8;
  y = 5;
 
  if (x == y) // here we are using the equality operator to determine whether both are equal or not
  {
    printf("The numbers are equal");
  } else // if the above condition returns false, the program will execute the below block
  {
    printf("The numbers are unequal");
  }
}

Output:

The numbers are unequal

The first step in the above code initializes the variables x and y. Subsequently, the program checks whether they are equal or not and prints the output message accordingly.

Comparing Characters Using Relational Operators

You can use all the above-discussed relational operators to compare the characters as per their ASCII value. For example, character ‘a’ has an ASCII value of 97, and character ‘b’ has an ASCII value of 98. When we compare ‘a’ and ‘b’, the program actually compares 97 and 98.

Here’s an example program that compares characters using relational operators.

#include <stdio.h>

int main() 
{
  char chr1 = 'b';
  char chr2 = 'a';
  // ASCII value of chr1 'b' is 98 and of 'a' is 97
  if (chr1 == chr2)
{
    printf("They are equal");
  } else if (chr1 > chr2) 
{
    printf("The first character is greater than second one");
  } else {
    printf("The first character is smaller than second one");
  }
}

Output:

The first character is greater than the second one

Summary Of Working Of Relational Operators In C

The following table briefly discusses all the relational operators in C. If P=5 and Q=3, then:

Operator

Operator meaning

Expression

Example

Less than

x<y

4<5 is evaluated to 1

Greater than

x>y

4>5 is evaluated to 0

Less than or equal to

<=

x<=y

4<=5 is evaluated to 1

Greater than or equal to

>=

x>=y

4>=5 is evaluated to 0

Equal to

==

x==y

4==4 is evaluated to 1

Not equal to

!=

x!=y

4!=4 is evaluated to 0

Understanding the relationship between two available values in C is easy with the implementation of relational operators. Based on the output of the expression (true or false), the program flow streamlines.

We expect that our tutorial will acquaint you with how to use relational operators in C++ and C. Along with reading this tutorial, mastering cutting-edge technical skills is also a significant aspect that aspirants should take into account, and this is what upGrad encourages!

You can pursue upGrad’s Master of Science in Computer Science, provided by Liverpool John Moores University and advance your career to stay ahead of your competitors in the tech industry. Pursuing this course benefits the aspirants with thorough guidance from industry experts and leading faculties. The live sessions, use cases, videos, practical projects, and extensive support are a few exceptional perks of pursuing this course. Completing this course makes you eligible to unlock outstanding career opportunities in software engineering.

 Enroll now to begin your journey! 

FAQs

1. What is the return value of all relational operators in C?

The return value of all relational operators is either false or true, represented as 0 or 1, respectively. A relational operator returns 1 if the comparison is true, else it returns 0. 

2. Do relational operators in C follow any precedence rules?

Yes, relational operators have lower precedence compared to logical and arithmetic operators in C. You should use parentheses to implement the required evaluation order while using any relational operators in a complex expression. 

3. Can you use relational operators to compare two strings?

You can’t use relational operators to directly compare strings.  Rather, you have to use string comparison functions like strcmp(). The relational operators only accept numerical values or single characters.

4. Can you chain multiple relational operators in an expression?

Yes, you can chain multiple relational operators in an expression. For instance, x > y && y > z is an expression that determines if x is greater than y and y is greater than z with the help of the logical AND operator (&&).

Leave a Reply

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