| by Arround The Web | No comments

C Relational and Equality Operators

Operators are the main foundation of any programming language as they help users perform mathematical and numerical operations on operands. With the help of these operators, the users will be able to operate operands. There are different types of built-in operators in C: arithmetic, logical, relational, bitwise, equality, and others.

This article will discuss relational and equality operators in the C programming language.

C Relational and Equality Operators

Relational and Equality Operators are C operators used to compare two values as they determine the relation between them. These operators include “equal to (=)”, “not equal to (!=)”, also called Equality operators. While they also include relational operators like “less than (<)”, “greater than (>)”, and a combination of both relational and equality operators like “less than or equal to (<=)” and “greater than or equal to (>=)”.

Let’s discuss each operator in detail.

1: Equal to Operator (=)

The equal to (=) operator is a type of equality operator used in a code to compare the values. It compares two values in a code and returns True if both are equal.

2: Not Equal to Operator (!=)

The not equal to (!=) operator in C language compares the two values and returns the result as True if both values are not equal.

3: Less than Operator (<)

This operator compares the two values and returns the statement as True if the first value is less than the second value. Otherwise, it considers it False.

4: Greater than Operator (>)

This is another relation operator that compares the two values and outputs the result to True if the first value is greater than the second value.

5: Less than or Equal to Operator (>=)

This operator checks the two values in a condition, and in the first case, it checks whether the first value is greater than the second value; if it’s not, it checks whether both are equal. If any condition meets, you will see the output as True. Otherwise, it outputs False.

6: Greater than or Equal to Operator (>=)

This is another useful operator that checks the comparison of two values. If the two values meet any condition greater or equal to, it generates the result as True, otherwise, it considers the statement as False.

Let’s follow up with a simple example below that relates to all these operators discussed above.

#include <stdio.h>

int main()

{

  int X =6, Y =10;

  // equal to

  if (X == Y)

printf("X is equal to Y\n");

  else

printf("X and Y are not equal\n");

  // not equal to

  if (X != Y)

printf("X is not equal to Y\n");

  else

printf("X is equal Y\n");

  // less than example

  if (X < Y)

printf("X is less than Y\n");

  else

printf("X is greater than or equal to Y\n");

  // greater than example

  if (X > Y)

printf("X is greater than Y\n");

  else

printf("X is less than or equal to Y\n");

  // lesser than equal to

  if (X <= Y)

printf("X is lesser than or equal to Y\n");

  else

printf("X is greater than Y\n");

  // greater than equal to

  if (X >= Y)

printf("X is greater than or equal to Y\n");

  else

printf("X is lesser than Y\n");

  return 0;

}

In the above code, two numbers X and X are given. The code checks each condition one by one. Those conditions that are met will print at the output shown below:

Output

In this way, you can use these relational and equality operators in C.

Conclusion

Relational and Equality Operators are C operators used to compare two values as they determine the relation between them. These operators include ”equal to (=)”, ”not equal to (!=)”, also called Equality operators. While they also include relational operators like less than (<), more significant than (>) and a combination of both relational and equality operators like less than or equal to (<=) and greater than or equal to (>=).

Share Button

Source: linuxhint.com

Leave a Reply