Relational Operators in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

Relational operators in C are vital for making comparisons and decisions in programming. These operators, including ==, !=, <, >, <=, and >=, allow you to assess relationships between variables. This guide explores their syntax and practical usage, enhancing your ability to create effective conditional statements and control program flow in C.

Types of Relational Operators in C

In C programming, relational operators allow you to compare values and make decisions based on the results of these comparisons. There are six main relational operators in C:

  • Equal to Operator (==)
  • Not Equal to Operator (!=)
  • Less than Operator (<)
  • Greater than Operator (>)
  • Less than or Equal to Operator (<=)
  • Greater than or Equal to Operator (>=)

Working of Relational Operators in C

  • Equal-to operator (==) compares two values. It returns true (1) if the values are equal and false (0) if they are not.
  • Not Equal to Operator (!=) The not equal to operator (!=) compares two values. It returns true (1) if the values are not equal and false (0) if they are equal.
  • Less than Operator (<) The less than operator (<) compares two values. It returns true (1) if the left value is less than the right value and false (0) otherwise.
  • Greater than Operator (>) The greater than operator (>) compares two values. It returns true (1) if the left value is greater than the right value and false (0) otherwise.
  • Less than or Equal to Operator (<=) The less than or equal to operator (<=) compares two values. It returns true (1) if the left value is less than or equal to the right value and false (0) otherwise.
  • Greater than or Equal to Operator (>=) The greater than or equal to operator (>=) compares two values. It returns true (1) if the left value is greater than or equal to the right value and false (0) otherwise.

Summary of Working of Relational Operators in C

OperatorDescriptionExample
Equal to (==)Checks if two values are equal.5 == 5 returns 1
Not Equal to (!=)Checks if two values are not equal.5 != 3 returns 1
Less than (<)Checks if the left value is less than the right value.3 < 5 returns 1
Greater than (>)Checks if the left value is greater than the right value.5 > 3 returns 1
Less than or Equal to (<=)Checks if the left value is less than or equal to the right value.3 <= 5 returns 1
Greater than or Equal to (>=)Checks if the left value is greater than or equal to the right value.5 >= 3 returns 1

Example of Relational Operators in C

Example of a Relational Operator in C with Integers

In this example, we use the greater than operator (>) to compare a and b. The program will print "a is greater than b" because the condition a > b is true.

Example of a relational operator in C with Integers

Example 1: Comparing Ages

In this example, we use the greater than operator (>) to compare Alice's age to Bob's age. If Alice's age is greater than Bob's age, the program prints "Alice is older than Bob."


Example 2: Exam Pass/Fail

In this example, we use the greater than or equal to operator (>=) to check if the examScore is greater than or equal to the passingScore. If the exam score is equal to or greater than the passing score, the program congratulates the student for passing the exam.

Example 3: Leap Year Here's an example that uses relational operators in a unique way to determine whether a given year is a leap year or not:

In this example, we use a combination of relational and logical operators to determine whether a given year is a leap year. A year is a leap year if it is divisible by 4, except for years that are divisible by 100 but not by 400. This unique example showcases how relational operators can be used to solve practical problems, in this case, a leap year calculation.

Practice Problems on Relational Operators in C

Practice Problem 1:

Which relational operator should be used to check if two variables, x and y, are not equal in C?

A) =! B) != C) <> D) ==

Answer: B) !=

Practice Problem 2:

Consider the following C code snippet:

What will be the output of this code?

A) Condition is true. B) Condition is false.

Answer: A) Condition is true.

Explanation: In this code, the >= operator is used to check if a is greater than or equal to b, which is true in this case (10 is greater than or equal to 5), so "Condition is true." will be printed.

Feel free to use these practice problems to test your understanding of relational operators in C.

FAQs

Q. What is the difference between the greater than/less than operator and the greater than and equal to/less than or equal to operator?

A. The greater than (>) and less than (<) operators strictly check if one value is greater or less than another, respectively. They do not include equality. The greater than or equal to (>=) and less than or equal to (<=) operators check if one value is greater than or equal to or less than or equal to another, respectively. They include equality.

Q. Does the equal to operator work with a single = sign?

A. No, the equal to operator in C uses two equal signs (==) to check if two values are equal. A single equal sign (=) is used for assignment, not for comparison. Using a single equal sign for comparison will result in a compilation error or unintended behavior in your code.

Conclusion

  • Relational operators, such as ==, !=, <, >, <=, and >=, are important for making decisions and comparisons in C programming.
  • Each relational operator has specific behavior, ensuring precise comparisons between variables, which are essential for creating reliable conditional statements.
  • Relational operators find practical use in various scenarios, from comparing ages and determining exam outcomes to solving complex problems like identifying leap years.