Relational Operators in C

Relational Operators in C

Making decisions and comparisons on data is important in C programs, which can be achieved efficiently using the relational operators. Relational operators are the symbols used to compare two values and make decisions based on the given conditions. In this article, we will discuss what relational operators are, types of relational operators, their importance, uses with different data types and conditional statements, and best practices for using relational operators in the C language.

Table of Contents:

What are Relational Operators in C?

Relational Operators in C are the operators that are used to compare two values. These operators return a Boolean result, as they return 1(true) if the condition is satisfied, returns 0(false) if the condition is not satisfied. They are also known as comparison operators.

Types of Relational Operators in C

There are six types of relational operations in C.

Operator Name Symbol Description Syntax
Equal to == It checks if two values are equal.  x == y
Not equal to != It checks if two values are not equal.  x != y
Greater than > It checks if the left value is greater than the right value.  x > y
Less than < It checks if the left value is less than the right value.  x < y
Greater than or equal >= It checks if the left value is greater than or equal to the right value.  x >= y
Less than or equal <= It checks if the left value is less than or equal to the right value.  x <= y

Let’s discuss each of these relational operators in brief, with the examples in C.

1. Equal to (==) Operator

The equal to (==) in C operator checks if two values are equal, and it returns true(1) if the values are equal, otherwise, it will return false(0).

Syntax:

expression1 == expression2

Example:

C

Output:

Equal to (==) Operator

The code shows that the equal to operator first checks if two integers x and y are equal or not and prints “x is equal to y” since x == y evaluates to be true, and then checks if x and z are equal or not and prints “x is not equal to z” since x == z evaluate to false.

2. Not Equal to (!=) Operator

The not equal to operator (!=) in C checks if two values are not equal, and it will return true(1) if the values are different; Otherwise, it will return false(0).

Syntax:

expression1 != expression2

Example:

C

Output:

Not Equal to (!=) Operator

The code shows the not equal to operator if two integers x and y are not equal, and then prints “x is not equal to y”, since x != y is evaluated as true.

3. Less Than (<) Operator

The less-than operator (<) in C checks if the left operand is smaller than the right operand. This operator will return true(1) if the condition is satisfied; Otherwise, it will return false(0).

Syntax:

expression1 < expression2

Example:

C

Output:

The code shows how the less-than operator checks if the left operand a is smaller than the right operand b. Since a<b evaluates to be true, then “a is less than b” is printed to the console as a result.

4. Greater Than (>) Operator

The greater-than (>) operator in C checks if the left operand is greater than the right operand. This operator will return true(1) if the condition is satisfied; Otherwise, it will return false(0).

Syntax:

expression1 > expression2

Example:

C

Output:

Greater Than ( data-lazy-src=

The code shows how the greater-than operator checks if the left operand a is greater than the right operand b. Since a>b evaluates to be true, then “a is greater than b” is printed to the console as a result.

5.  Less Than or Equal To (<=) Operator

The less than or equal to operator (<=) in C checks if the left operand is less than or equal to the right operand and returns true(1) if the condition is true; Otherwise, it will return false(0).

Syntax:

expression1 <= expression2

Example:

C

Output:

The code shows how the less than or equal to operator checks if the left operand a is less than or equal to the right operand b. Since a <= b evaluates to be true, then “a is less than or equal to b” is printed to the console as a result.

6. Greater Than or Equal To (>=) Operator

The greater than or equal to operator (>=) in C checks if the left operand is greater than or equal to the right operand and returns true(1) if the condition is true; Otherwise, it will return false(0).

Syntax:

expression1 >= expression2

Example:

C

Output:

Greater Than or Equal To ( data-lazy-src=

The code shows how the greater than or equal to operator checks if the left operand a is greater than or equal to the right operand b. Since a >= b evaluates to be true, then “a is greater than or equal to b” is printed to the console as a result.

Importance of Relational Operators in C

  • Decision Making: These operators are used in the conditional statements such as if, if-else, and switch for decision making.
  • Loop Control: Relational operators can be used with the loops, such as for, while, and do-while, to check when to continue or terminate the iterations.
  • Comparison: They help to compare values in programming algorithms.
  • Logical Operations: These operators can be combined with the logical operators to form the complex conditions to perform logical operations.
  • Input Validation: They provide proper input validations so that the user can give correct input.

Relational Operators in Conditional Statements in C

Relational operators are also used in the conditional statements, such as if, if-else, else-if, and switch, to make decisions based on comparisons.

Example:

C

Output:

Relational Operators in Conditional Statements in C

The code shows that the >= operator checks if the age is greater than or equal to operator 18, and since the condition is true, then “You are eligible to vote” is printed to the console.

Use of Relational Operators in C with Different Data Types

These operators can also be used with different data types such as integer, char, and float. 

1. With Integers

The relational operators are used with the integers to compare the values of two integers.

Example:

C

Output:

With Integers

The code shows how the relational operator is used to compare two integers with values x=10 and y=20, and then to print the result to the console.

2. With Floating-Point Numbers

The relational operators can also be used with the floating point numbers to compare the two decimal values.

Example:

C

Output:

With Floating-Point Numbers

The code shows how the relational operator is used with floating point numbers to compare two decimal values, x = 5.5 and y = 3.2, and then to print the output to the console.

3. With Characters

The relational operators are also used with the characters to compare the ASCII values of the characters.

Example:

C

Output:

With Characters

The code shows how the relational operator is used with the characters to compare two ASCII values, ch1=A and ch2=B, and then to print the output to the console.

Best Practices for Using Relational Operators in C

  1. You must use the variables of the same data type to compare them, otherwise, they will be incompatible.
  2. Always use clear naming to avoid confusion because C is a case-sensitive programming language.
  3. You should avoid using == due to precision loss issues while comparing the floating-point numbers.
  4. You should always check that you have written the relational operators in the correct form.
  5. Always use the relational operators within conditional statements to control the program flow.
  6. You should comment your code if there is complex logic in your code to make it understandable.

Conclusion

We can conclude that the relational operators in C are important for decision-making and comparison. These operators help you to compare values, variables, and expressions. They give true or false as a result based on the conditions. Relational operators are also used with different data types in C. So, by understanding how the relational operators work and their importance in best practices, you can easily use them in your C programming.

FAQs on Relational Operators in C

Q1. What do relational operators do in C?

The relational operators in C compare values and return true (1) or false (0) as a result.

Q2. Can I use relational operators with different data types?

Yes, you can use relational operators with integers, floats, characters, and pointers.

Q3. What is the difference between = and ==?

The difference between = and == is that = assigns a value, and == checks for equality.

Q4. Is == reliable for floats?

No, it is not reliable due to precision issues.

Q5. Are variable names case-sensitive in comparisons?

Yes, C because it treats x and X as different variables.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner