Skip to content

Relational Operators in C

Relational operators in C are used to compare the relationship between two operands. They evaluate to a boolean value (1 for true or 0 for false) based on the result of the comparison. Relational operators are fundamental for making decisions and controlling the flow of execution in C programs. Let’s explore the relational operators in detail:

Types of Relational Operators:

  1. Equal to (==):
    • The equal to operator (==) checks if two operands are equal.
    • It returns 1 if the operands are equal and 0 otherwise.
    • Example: int result = (5 == 5); (result will be 1)
  2. Not equal to (!=):
    • The not equal to operator (!=) checks if two operands are not equal.
    • It returns 1 if the operands are not equal and 0 if they are equal.
    • Example: int result = (5 != 3); (result will be 1)
  3. Greater than (>):
    • The greater than operator (>) checks if the left operand is greater than the right operand.
    • It returns 1 if the left operand is greater and 0 otherwise.
    • Example: int result = (5 > 3); (result will be 1)
  4. Less than (<):
    • The less than operator (<) checks if the left operand is less than the right operand.
    • It returns 1 if the left operand is less and 0 otherwise.
    • Example: int result = (3 < 5); (result will be 1)
  5. Greater than or equal to (>=):
    • The greater than or equal to operator (>=) checks if the left operand is greater than or equal to the right operand.
    • It returns 1 if the left operand is greater than or equal to the right operand and 0 otherwise.
    • Example: int result = (5 >= 5); (result will be 1)
  6. Less than or equal to (<=):
    • The less than or equal to operator (<=) checks if the left operand is less than or equal to the right operand.
    • It returns 1 if the left operand is less than or equal to the right operand and 0 otherwise.
    • Example: int result = (3 <= 5); (result will be 1)

Usage:

  • Relational operators are commonly used in conditional statements (if, else, switch) and loops (while, for) to control the flow of execution based on certain conditions.
  • They can also be used in expressions to evaluate conditions and assign the result to variables or perform other operations.

Operator Precedence:

  • Relational operators have lower precedence than arithmetic operators but higher precedence than logical operators.
  • Parentheses can be used to specify the order of evaluation and override the default precedence.

Examples:

int x = 5, y = 3; int result1 = (x == y); // result1 will be 0 (false)

int result2 = (x > y); // result2 will be 1 (true)

int result3 = (x != y); // result3 will be 1 (true)

Conclusion:

Relational operators in C provide a way to compare the relationship between two operands. They are essential for making decisions and controlling the flow of execution in programs. By understanding how to use relational operators and their behavior, programmers can write code that performs conditional logic and responds to various situations effectively.