Relational Operators

Relational Operators

Published by: Nuru

Published date: 21 Jun 2021

Relational Operators Photo

Relational Operators

  • Relational operators are used to comparing two similar operands, and depending on their relation, take some actions.
  • They compare their LHS operand with their RHS operand for lesser than, greater than, lesser than or equal to, greater than or equal to, equal to, or not equal to relations.
  • The value of a relational expression is either 1 (if the condition is true) or 0 (if the condition is false).

Relational Operators

Note:-

  • The operators == and != are also called equality operators.
  • When arithmetic expressions are used on either side of a relational operator, the arithmetic expressions will be evaluated first and then the expressions will be evaluated first, and then the results are compared.
  • Relational operators are used in decision-making statements like if.....else statements.

The Precedence of Relational Operators

  • The precedence of relational operators determines the order in which they are performed in a multiple-operator expression.
  • However, We can use parentheses to modify precedence in expressions that use relational operators.
  • First, all the relational operators have lower precedence than arithmetic operators.
  • Thus, if we write the following, 2 is added to x, and the result is compared to y: (x + 2 > y) equivalent to ((x + 2) > y) which is a good example of using parentheses for the clarity:
  • Next, C determines whether x is equal to the 1 or 0 obtained in the first step.

Some Examples

#include
#include
void main()
{
int a=10,b=28,c=10;
clrscr();

printf("a %d \t a>b => %d \t a==c => %d", ab, a==c);
printf("\na<=b => %d \t a>=b => %d \t a!=b => %d", a<=b, a>=b, a!=b);

getch();
}

 

#include

inta;

main()

{

a = (5 == 5);                     /* Evaluates to 1 */

printf("\na= (5 == 5) evaluates a = %d", a);

a = (5 != 5);                     /* Evaluates to 0 */

printf("\na= (5 != 5) evaluatesa= %d", a);

a = (12 == 12) + (5 != 1);   /* Evaluates to 1 + 1 */

printf("\na= (12 == 12) + (5 != 1) evaluates a = %d\n", a);

return 0;
}