Relational Operators in C

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
By Prateek Narang
Free
star5
Enrolled: 1000
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
Prateek Narang
Free
5
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Relational Operators

Overview

Relational Operators are the type of operators used in a programming language that helps us compare any two entities, like two integers, characters, and many more. They always give the result in 1 or 0, where 1 means that the comparison is True and 0 means that the comparison is False (i.e., opposite of that).

In simple words, we can say that the output produced by the relational operators in an expression is boolean, as in boolean, there are only two values: true or false.

Scope

In this article, we will learn all about the Relational Operators in C. First of all, we will discuss all the types of relational operators available to us in the C language, then we will see some examples and how we can use them in our code. We will also discuss the importance of precedence while simplifying an expression. In the end, we will see how characters and strings are compared using relational operators.

List of Relational Operators

NameSymbolDefinition
Equality Operator==This relational operator tests the equality relation between two operands and returns 1 if both are equal otherwise returns 0. For eg- (4 == 4) -> true and (4 == 5) -> false
InEqualty Operator!=This relational operator tests the Inequality between two operands and returns 1 if both are not equal otherwise returns false. This is the opposite of equality operator. For eg- (3 != 4) -> true and (4 != 4) -> false
Less than Operator<This relational operator tests that if one value is strictly less than the other or not and returns 1 if the first value is less otherwise returns 0. For eg- (2 < 3) -> true and (2 < 1) -> false
Greater than Operator>This relational operator tests that if one value is strictly greater than the other one or not and returns 1 if the first value is strictly greater otherwise returns 0. For eg- (4 > 3) -> true and (2 > 3) -> false
Less Than or Equal to Operator<=This relational operator tests that if one value is less than or equal to the other one or not and returns 1 if the first value is less than equal to otherwise, returns 0 . For eg- (4 <= 4) -> true and (4 <= 3) -> false
Greater than or Equal to Operator>=This relational operator tests that if one value is greater than or equal to the other one or not and returns 1 if the first value is greater than equal to otherwise returns false. For eg- (4 >= 3) -> true and (2 >= 3) -> false

Examples And Implementation of Relational Operators

As we have discussed, all the types of relational operators in C Programming language. Now let's see some examples illustrating how they can be implemented in the code.

Equality and InEquality Relational Operators

Equality and Inequality operators are opposite of each other. The equality operator returns true when both entities are equal, but the inequality operator returns true when both entities are not equal.

Here is the implementation of both equality and inequality operators in the C language.

Code

Output

Code

Output

Less and Less than or Equal Relational Operators

The difference between less than and less than or equal operators is that the less than operator returns true only when the left operand is strictly less than the right one otherwise, it always returns false but in the case of less than or equal operator, it returns true if the left operator is less than or equal to the right operator otherwise it returns false.

Here is the implementation of both less than and less than or equal operators in C language.

Code

Output

Code

Output

Greater than and Greater than or Equal Relational Operators

The only difference between these two of them is that the greater than operator returns true only when the left operand is strictly greater than the right one but in the case of the greater than or equal relational operator, it only returns false when the first operand is strictly less than second operand.

Here is the implementation of both greater than and greater than or equal relational operators in C language.

Code

Output

Code

Output

Result Of A Relational Expression

All the relational operators in the C language always return boolean value which means either true or false.

Let's take an example of a relational expression, 10==9. Now here, the equals to the operator will either return a true or false value depending upon whether the expression is correct or incorrect. Similarly, every other relational operator first checks the condition and returns the required output in boolean type.

Here true means that the relation is satisfied and false means the relationship is not satisfied.

How A Program Treats True And False

The return type of all the relational operators in C language is always in the form of boolean, i.e., either true or false. So the program stores the true value as 1 and the false value as 0.

Relational Operator Precedence and Relationship With Other Operators

Precedence plays a significant role while determining the result of any expression. For example, suppose an expression consists of multiple operators. In that case, the operator with high precedence should be treated first, and the output of that operator is used to evaluate the result of the rest expression. Here is the precedence table of all operators in the decreasing order of their precedence.

TypeOperator
Postfix(), [], ++, --
Multipicative* , / , %
Additive+ , -
Relational< , <= ,> ,>=
Equality== ,!=
Logical AND&&
Logical OR| |
Assignment Operator= ,+=, -= , *=

Example of Precedence

As we have seen in the precedence table, let's see an example in which precedence plays an important role.

Below is the code snippet of precedence example.

Code

Output

Let's see one more example of precedence.

Code

Output

Comparing Characters With Relational Operators

The relational operator in C language compares all the characters according to their ASCII value.

For example, 'a' has its ASCII value of 97 and 'b' has '98', so when we compare 'a' with 'b', in general, 97 and 98 are compared.

Now let's see the code for comparing characters.

Code

Output

Note: Let us now compare the characters 'a' and 'A'. For a moment, one might argue that 'a' is smaller than 'A'. So, 'a' must be less than 'A'. Sorry, but this is incorrect. As mentioned, characters are compared based on their ASCII value, and the ASCII value of 'a' is 97 and 'A' is 65. Therefore, 'a' > 'A'.

Let us see this in the program

Code

Output

Comparing Strings Or Character Array With Relational Operators

In programming languages, strings are compared from left to right using the relational operator. A relational operator in C language compares two strings character by character from left to right. The comparison is made till a match is not found or one of the strings is exhausted.

For example: If we compare the strings "abcd" and "abed". Firstly the characters at the 0th index of both strings get compared, and since both of them are equal, it moves to the next index, and when it comes to the 2nd index, it is found that the characters are distinct and output the result accordingly.

Now let's see a code implementation for comparing two strings.

Code

Output

Special Case

Let's assume two strings as "abc" and "abcd". Here the first 3 characters of both the strings are equal, and the first string has no other character compared with the second string. The string with a smaller length will be treated as lexicographically small than the other one, therefore, in this case string "abc" will be less than the string "abcd".

Example:

Code

Output

Character Testing Functions

There are many inbuilt Character Testing Functions in C language, predefined. First of all, to access them, we need to add a header file named #include <ctype.h>.

The return type of these character testing functions is also a boolean value (i.e., true or false);

Below is the list of all the character testing functions in C Language:

Character Testing FunctionDescription
isdigitThis function checks whether the character is a digit (0-9) or not
islowerThis function checks whether the character is a lowercase letter or not
isupperThis function check whether the character is an uppercase letter or not
isspaceThis function checks whether the character is a white space or not (e.g., tab, space, newline)
isprintThis function checks whether a character is printable or not
isalphaThis function checks whether the character is an alphabet or not

Let's see an example to understand the usage of the above functions.

Code

Conclusion

  1. Relational operators in C are the type of operators used to compare two operands.
  2. The return type of all the relational operators in C language is a boolean value.
  3. Relational operators in C are also used to compare characters and strings based on their ASCII value.
  4. Relational Operator in C language follows the precedence while determining the result.