< Prev
Next >

# C - Relational Operators

There are six relational operators in C -
• <(less than operator)
• >(greater than operator)
• <=(less than equals to operator)
• >=(greater than equals to operator)
• ==(equals to operator)
• !=(not equals to operator)

## < (less than operator)

This operator is also known as less than operator. - It compares between the values of two operands around it. < operator returns a boolean value true when operand on its left is less than the operand on its right, otherwise it returns false.

## A coding example with < operator

``````/* A coding example with < operator */

#include<stdio.h>

int main()
{
if(5 < 10)		/* comparing an int to an int */
printf("5 is less than 10 \n");

if('a' < 'b')		/* comparing a character with a character */
printf("a is less than b \n");

if(100 < 'z')		/* comparing an int with ASCII value of a character */
printf("100 is less than z \n");

if( 100.5 < 100.4)	/* comparing a double with double */
printf("100.5 is less than 100.4 \n");

if(100 < 100.001)	/* comparing an int with a double */
printf("100 is less than 100.001");

return 0;
}``````

## Output is

``````5 is less than 10
a is less than b
100 is less than z
100 is less than 100.001``````

## > (greater than operator)

This operator is also known as greater than operator. - It compares between the values of two operands around it. > operator returns a boolean value true when operand on its left is greater than the operand on its right, otherwise it returns false.

## A coding example with > operator

``````/* A coding example with > operator */

#include<stdio.h>

int main()
{
if(10 > 10)		/* comparing an int with an int */
printf("10 is greater than 10 \n");

if('b' > 'a')		/* comparing a character with a character */
printf("b is greater than a \n");

if(200 > 'z')		/* comparing an int with an ASCII value of A character */
printf("200 is greater than z \n");

if( 100.5f > 100.4999)	/* comparing a float with a double */
printf("100.5 is greater than 100.4999 \n");

if(100 > 100.001)	/* comparing an int with a double */
printf("100 is greater than 100.001");

return 0;
}``````

## Output is

``````b is greater than a
200 is greater than z
100.5 is greater than 100.4999``````

## A coding example with <= operator

``````/* A coding example with <= operator */

#include<stdio.h>

int main()
{
if('a' <= 'b')		//comparing a character with another character
printf("a is less than or equal to b \n");

if('a' <= 1)		//comparing a character's ASCII value with int
printf("a is less than or equal to 1 \n");

if('z' <= 'z')
printf("z is less than equal to z \n");

if(30.5 <= 31)		//comparing an double value with int
printf("30.5 is less than or equal to 31 \n");

if(20.5 <= 20.6)	//comparing an double value with int
printf("20.5 is less than or equal to 20.6 \n");

if(10.5 <= 10.5)	//comparong a double with a double
printf("10.5 is less than or equal to 10.5 \n");

if(5 <= 5)		//comparing an int to int
printf("5 is less than or equal to 5 \n");

if(6 <= 6.0)		//comparing a int with a double
printf("6 is less than or equal to 6.0 \n");

if(10 <= 6)		//comparing a int with a double
printf("10 is less than or equal to 6 \n");

return 0;
}``````

## Output is

``````a is less than or equal to b
z is less than equal to z
30.5 is less than or equal to 31
20.5 is less than or equal to 20.6
10.5 is less or equal to 10.5
5 is less than or equal to 5
6 is less than or equal to 6.0``````
Note: There should never be any space between < and = of <= operator, or a compile error is thrown.

## >= (greater than equals to operator)

This operator is also known an greater than equal to operator. It compares between the values of two operands around it and it returns a boolean value, true, if either of these conditions are true -
• If the operand of the left of >= operator is greater than operand on the right of >=, or,
• If the operand on the left of >= is equal to operand on its right.
• if neither of above conditions are met then this operator return false.

``````#include<stdio.h>

int main()
{
if('z' >= 'b')		/* comparing a character with another character */
printf("z is greater than or equal to b \n");

if('a' >= -1)		/* comparing a character's ASCII value with int */
printf("a is greater than or equal to 1 \n");

if('z' >= 'z')
printf("z is greater than equal to z \n");

if(50.5 >=50)		/* comparing a double value with int */
printf("50.5 is greater than or equal to 50 \n");

if(19.7 >=21.7)		/* comparing a double value with a double */
printf("19.7 is greater than or equal to 21.7 \n");

if(10.5 >= 10.5)	/* comparing a double with a double */
printf("10.5 is greater or equal to 10.5 \n");

if(5 >= 5.0)		/* comparing an int to int */
printf("5 is greater than or equal to 5.0 \n");

if(6 >= 10)		/* comparing a int with a double */
printf("6 is greater than or equal to 10 \n");

if(100.5 >= 'a')	/* comparing a double with a char */
printf("100.5 is greater than or equal to a");

return 0;
}``````

## Output is

``````z is greater than or equal to b
a is greater than or equal to 1
z is greater than equal to z
50.5 is greater than or equal to 50
10.5 is greater or equal to 10.5
5 is greater than or equal to 5.0
100.5 is greater than or equal to a``````
Note: There should never be any space between > and = of >= operator, or a compile error is thrown.

## == (equals to operator)

This operator is called equals to operator. It compares between the values of two operands by checking for their equality. It returns a boolean value true when operands have equal values otherwise it returns false.

## A coding example with == operator

``````/* A coding example with == operator */

#include<stdio.h>

int main()
{
if('z' == 'z') 		/* comparing a character to another character */
printf("z is equal to z \n");

if(50.5 == 50.5)	/* comparing an double value with double */
printf("50.5 is equal to 50.5 \n");

if(100 == 100.001)	/* comparing an int with a double */
printf("100 is equal to 100.001 \n");

if(99.9999 == 99.999)	/* comparing a double with a double */
printf("99.9999 is  equal to 99.999 \n");

if(5 == 5.0)		/* comparing an int to double */
printf("5 is equal to 5.0 \n");

if(10 == 10)		/* comparing an int to int */
printf("10 is equal to 10 \n");

if(97 == 'a')		/* comparing an int to ASCII value of character */
printf("97 is equal to a");

return 0;
}``````

## Output -

``````z is equal to z
50.5 is equal to 50.5
5 is equal to 5.0
10 is equal to 10
97 is equal to a``````
Note: There should never be any space between = and = of == operator, or a compile error is thrown.

## != (not equals to operator)

This operator is also knows an not equal to operator. It compares between the values of its two operands by checking for their inequality. It returns a boolean value true when operands have unequal values otherwise it returns false.

## A coding example with != operator

``````/* A coding example with != operator */

#include<stdio.h>

int main()
{
if('z' != 'a') 		/* comparing a character to another character */
printf("z is not equal to a \n");

if(100 != 100.001)	/* comparing an int with a double */
printf("100 is not equal to 100.001 \n");

if(99.9999 != 99.999)	/* comparing a double with a double */
printf("99.9999 is  not equal to 99.999 \n");

if(5.00 != 5.0)		/* comparing an int to double */
printf("5.00 is not equal to 5.0 \n");

if(11 !=10)
printf("11 is not equal to 10 \n");

if(98 != 'a')		/* comparing int to ASCII value of a character */
printf("98 is not equal to a");

return 0;
}``````

## Output -

``````z is not equal to a
100 is not equal to 100.001
99.9999 is  not equal to 99.999
11 is equal to 10
98 is equal to a``````
Note : There should never be any space between ! and = of != operator, or a compile error is thrown.

< Prev
Next >
< Compound Assignment Operators in C
Logical Operators in C >