1. Home
  2. Docs
  3. C Programming Guides
  4. Operators
  5. Relational Operators

Relational Operators

Relational operators check if a specific relation between two operands is true. The result is either 1 (which means true) or 0 (which means false). Usually it is used to control the flow of program in control statements. The result can also be used as a variable later.

Equals “==”

It checks whether the operands (left and right) are equal or not (true: if equals and false : if not equal)

1 == 0; /* evaluates to 0. */
1 == 1; /* evaluates to 1. */
int x = 5;
int y = 5;
int *xptr = &x, *yptr = &y;
xptr == yptr; /* evaluates to 0, the operands hold different location addresses. */
*xptr == *yptr; /* evaluates to 1, the operands point at locations that hold the same value. */

Not Equals “!=”

It checks if the operands are not equal (true : if not equal & false : if equals)

1 != 0; /* evaluates to 1. */
1 != 1; /* evaluates to 0. */
int x = 5;
int y = 5;
int *xptr = &x, *yptr = &y;
xptr != yptr; /* evaluates to 1, the operands hold different location addresses. */
*xptr != *yptr; /* evaluates to 0, the operands point at locations that hold the same value. */

Not “!”

It checks if the the object is equal to 0 and sometimes used to negate the boolean value

!someVariable;
// this has the same effect as that of 
someVariable == 0;

someBool = !someBool; // this negates the value of someBool (value becomes true if it is false and becomes false if it is true)

Greater than “>”

This checks if the left operand is greater than right operand

int i = 10;
bool res = i > 20; // returns false as i is not greater than 20
res= i > 5; // return true as i is greater than 5
res = i > 10; // return false as i is not greater than 10

Less Than “<“

This checks if the left operand is less than right operand

int i = 10;
bool res = i < 20; // returns true as i is less than 20
res= i < 5; // return false as i is not less than 5
res = i < 10; // return false as i is not less than 10

Greater than or equal to “>=”

This checks if the left operand is greater than right operand

int i = 10;
bool res = i >= 20; // returns false as i is not greater than or equal to 20
res= i >= 5; // return true as i is greater than or equal to 5
res = i >= 10; // return true as i is equal to 10

Less than or equal to “<=”

This checks if the left operand is less than right operand

int i = 10;
bool res = i <= 20; // returns true as i is less than 20
res= i <= 5; // return false as i is not less than 5
res = i <= 10; // return true as i is equal to 10
Was this article helpful to you? Yes No

How can we help?

Leave a Reply

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