Code Factory — Relational Operators

Shafi Sahal
Geek Culture
Published in
3 min readApr 5, 2021

In programming, very often there arises a need to evaluate expressions. We may need to check whether the values stored in two variables are equal or whether the value in one variable is greater than the value in the other variable or vice versa and so on. We use relational operators for such operations.

Boolean Data Type

There is one special data type known as boolean. This data type can only store either of the two values: true or false. To use boolean data type in C, we have to import a header file known as ‘stdbool.h’. We know how to add header files.

//Adding stdbool.h file
#include<stdbool.h>
//This is how to declare a boolean in C
bool c;
//We can only assign true or false to c
c = true;

We can only assign true or false to c.

==

This is the ‘Equal to’ operator. This operator checks whether the values of operands on each side of the operator are equal.

int a, b;
bool c;
a = 3;
b = 5;
c = a == b;

Here c will contain false since a is not equal to b. If a and b were equal c will contain true.

!=

This is the ‘Not Equal to’ operator. It checks whether the values of the operands on each side of the operator are not equal. If the values are equal, it gives false and true otherwise.

int a, b;
bool c;
a = 3;
b = 5;
c = a != b;

Here c will contain true since a and b are not equal. If a and b were equal c will contain false

>

This is the ‘Greater than’ operator. It checks whether the value of the operand on the left is greater than the value of the operand on the right.

int a, b;
bool c;
a = 3;
b = 5;
c = a > b;

Here c will contain false since a is less than b. If a were greater than b, c will contain true

>=

This is the ‘Greater than or equal to’ operator. This operator checks whether the value of the operand on the left is either greater than or equal to the value of the operand on the right.

int a, b;
bool c;
a = 3;
b = 5;
c = a >= b;

Here c will contain false since a is less than b. If a and b were equal or if a were greater than b, c will contain true.

<

This is the ‘Less than’ operator. It checks whether the value of the operand on the left is less than the operand on the right.

int a, b;
bool c;
a = 3;
b = 5;
c = a < b;

Here c will contain true since a is less than b. If a were greater than b, c will contain false.

<=

This is the ‘Less than or equal to’ operator. It checks whether the value of the operand on the left is either less than or equal to the value of the operand on the right.

int a, b;
bool c;
a = 3;
b = 5;
c = a <= b;

Here c will contain true since a is less than b. c will also contain true if and b were equal. c will contain false only when a is greater than b.

Previous => Code Factory — Arithmetic Operators

--

--

Shafi Sahal
Geek Culture

Developer, adventure lover and a truth seeker. Like to write about topics from a unique perspective. Twitter: https://twitter.com/_shafisahal.