Relational and Equality Operators in C

For Complete YouTube Video: Click Here

In this class, we will try to understand Relational and Equality Operators in C.

We have already discussed Arithmetic, Assignment, Increment-Decrement, Bitwise, Shift, and Ternary Operators.

Before understanding Relational and Equality operators, we will understand Logical Expression.

Logical Expression

A Logical Expression is a statement that evaluates to either “true” or “false.”

Some expressions evaluate either 1 [True] or 0 [False] as the output in C.

Using three operators, we can construct a Logical Expression.

  1. Relational Operator [<, >, <=, >=]
  2. Equality Operators [==, !=]
  3. Logical Operators [&&, ||, !]

Relational and Equality Operators in C

The image below shows the Relational and Equality Operators in C.

Relational and Equality Operators in C
Relational and Equality Operators in C

Relational Operators [ <, >, <=, >= ]

Relational Operators compare the values of the operands and produce either 0 (False) or 1 (True) as the output.

Relational operators are also called comparison operators.

The relational operators are [< Less than, > Greater than, <= Less than or equal to, and >= Greater than or equal to ].

Consider the following expression with relational operators and guess the outputs generated by each expression.

4<5, 23>=96, 3 < 54.

The above expressions generate an output of 1, 0, 1.

We should be very careful while writing the relational expression.

For example, consider the simple program as shown below.

int a=2, b=3, c=4;

We intend to check a<b<c.

Mathematically it is like 2<3<4.

But the compiler will first execute [2<3] and produces the output as 1, and now 1<4 is compared.

The above comparison is not true.

Equality Operators [ ==, != ]

Equality Operators compare the equality of the operands and produce either 0 (False) or 1 (True) as the output.

The equality operators are [== Equal to, != Not Equal to].

We have to understand that [== is equal to] operator and [=] is the assignment operator.

Some example of the expressions using equality operators 10 == 10, 9 != 10.

The outputs of the above expressions are 1, 0.