Relational Operators in C (AKA Comparison Operators)

In this section, we will learn what the relational operators are and how to use them in C.

Note: Before reading this section, we’re assuming that you already read the operator section.

What are the Relational Operators? (Comparison Operators)

Comparison operators are those operators that are used to compare two constants, variables, or expressions. The result of this comparison is either true or false.

For example:

Let’s say we have a variable named `jackAge` and the value of this variable is 20. We can run different sorts of comparisons on this variable, like:

jackAge == 21 // This comparison is used to see if the value assigned to the jackAge variable is equal to the value 21 or not. Well, the result will be false because the value of the variable is 20 which is not equal to 21.

jackAge > 21 //This is the Greater-Than operator which we used to see if the left-hand operand is greater than the right-hand operand. So here, because the value of the jackAge variable is less than the value 20, the result will be false then.

Now, let’s move on and see the list of comparison operators supported in C.

List of Relational Operators in C

Here’s an introductory list of comparison operators:

Operator Symbol
Equal ==
Not equal !=
Greater than >
Less than <
Greater or equal to >=
Less than or equal to <=

Conditional Statements

Comparison operators are mostly used within conditional statements.

In short, conditional statements are those statements that their executions depend on a condition to be true! The conditional statements also have a block of instructions as well. Now, if the result of the condition of these types of statements was true, then the body part of these statements will be executed. Otherwise, if the condition resulted false, then the instructions within the body of these statements will be ignored and our programs will continue to run the instructions after the conditional statements.

In this section, we need to use one of these condition-statements in order to explain comparison-operators with better details. For this reason, we start with the `if-statement` which is a conditional statement and after that, we’ll use the if statement in order to explain how the comparison operators can be used in C programs.

Conditional Statements: If statement

You can learn more about the `if` statement in the if-statement section but in short, its use case in programming is the same as when we use it in the real world.

For example, we say if that person’s age is more than 21 then she can go to a Bar otherwise she is not allowed etc.

Basically, the if statement has two parts:

  • The condition part.
  • The block part.

The condition part is where we use the comparison operators to create a condition which will be checked first when our program is running.

On the other hand, the block part of an if-statement is where we put a set of instructions to be executed “if” the result of the condition turns out to be true.

Here’s the structure of the `if` statement:

if (condition){
//body of the if-statement where code is and will be run if the condition is true
}else{
//the code here will only run if the condition is not true.
}

if: this is the keyword that we use for starting an `if-statement`.

`()`: followed by `if` keyword is the parentheses where we define the condition (expression) that we want to be true in order to run the instructions within the body of the `if` statement. This is where comparison operators can be used.

`{}`: the braces that come after `()` is the body of `if-statement` and we can declare instructions to be run if the condition was true.

`else`: if it turns out that the defined condition is not true, then the instructions within the body of `else` statement will run instead. The body of `else` is within the braces` {} ` that comes after the `else` keyword. You can think about the `else` statement as the plan B. Basically, if the condition of the related `if-statement` failed and ended up being false, then the instructions we define within the body of the else statement are those that should be as a result.

If statement Example:

#include <stdio.h>

int main() {


    int age = 13;

    //Is the person allowed to go to a Bar?
    if (age < 21){
        //This line will run if the condition is true.
        printf("No, this person can't go to a Bar. ");
    }else{
        // In case the condition was not true, the instruction in this body will  run. 
        printf("Yes, this person can go to a Bar. ");
    }
    return 0;
}

Result:

No, this person can't go to a Bar.

In this example we’ve used the `if` statement to see if the value in the `age` variable is less than 21 or not via less-than-operator `<` (which you’ll learn more about it later in this section).

The value in the `age` variable is not greater than the value 21 and so the condition of the `if` statement is true and the instructions in its body will run.

Note: depending on the result of the condition, either `if` statement or `else` statement’s body will run, but not both!

There are more in the `if` statement, but for now, this is going to be enough. In the if-statement section, you’ll learn more about this conditional statement.

Note: because in this section we’re focusing on explaining the comparison-operators, we won’t use complex if-statements for the sake of simplicity.

C Equal `==` Operator:

We use the equal `==` operator to see if two values are equal in value or not. For example, let’s say we have two variables and now want to know whether the values of the variables are equal or not. Well, this operator can help us in a situation like this.

Note that the result of the `equal` operator as well as the rest of comparison operators that you’ll see in just a bit, is a type of value that is known as boolean. Basically, it will be a true, false type of value.

The value `true` means the comparison operation succeeded and the value false means otherwise.

For example, if we say 10 == 10, this is a successful comparison because both values are equal to each other. But if we say 10 == 30, this is a failed operation because the value 10 is not equal to the value 30.

Note: the body of the conditional statements like the `if` statement runs only if the result of a comparison or a boolean expression becomes true.

Equal `==` Operator Example:

#include <stdio.h>

int main() {


    int age = 13;

    int second = 8;
    if (age == (2* second -5 )){
        printf("Yes, the value of age is equal to the result of the expression.");
    }else{
        printf("The result of the expression is not equal to the value of age-variable. ");
    }
    return 0;
}

Result:

Yes, the value of age is equal to the result of the expression.

In the example above, we’ve used the equal `==` operator to check whether the result of the `(2* second -5 )`expression is equal to the value assigned to `age` variable and because the result is `true` the body of the `if` statement ran and we got the result you see above on the output.

C Not Equal `!=` Operator:

This operator is exactly on the opposite side of the equal `==` operator! The Not-Equal `!=` operator is used to check whether two values (constants, variables and expressions) are not equal! If the two values are not equal, then the result is true otherwise, it will be false.

Not Equal `!=` Operator Example:

#include <stdio.h>

int main() {


    int jackAge = 30;

    int ellenAge = 33;
    if (jackAge != ellenAge){
        printf("These two people are not in the same age.");
    }else{
        printf("These two people were born at the same year ");
    }
    return 0;
}

Result:

These two people are not in the same age.

In the example above, we’ve checked to see if the two variables `jackAge` and `ellenAge` contain different values via the use of Not-Equal `!=` operator. If they have different values (which they have in this example) then the result of the `if` statement becomes true and the instructions in its body will run.

C Greater Than `>` Operator:

We use the greater-than `>` operator to compare two values (constants, variables, expression) and see if the value on the `left` side of this operator is greater than the value on the `right` side or not. If the left side value was greater, the result is true otherwise, we will get a false result.

Greater Than `>` Operator Example:

#include <stdio.h>

int main() {


    int jackAge = 30;

    int ellenAge = 33;
    if (jackAge > ellenAge){
        printf("Yes, Jack is older than Ellen");
    }else{
        printf("No, Jack is younger than Ellen ");
    }
    return 0;
}

Result:

No, Jack is younger than Ellen

In the example above, we’ve tried to see if Jack `jackAge` is older than Ellen `ellenAge` via the greater-than `>` operator within the condition of the `if` statement. But in fact Ellen is older than Jack and so the result of this comparison is false and the body of `if` statement in this case will not run, but the body of the `else` statement will do. And that’s how we’ve got the result you saw above.

C Less Than `<` Operator:

We use less-than `<` operator to compare two values (constants, variables, expression) and see if the value on the `left` side of the comparison is less than the value on the `right` side or not. If the left value was lesser, the result is true otherwise, we will get a false value.

Less Than `<` Operator Example:

#include <stdio.h>

int main() {


    int jackAge = 30;

    int ellenAge = 33;
    if (jackAge < ellenAge){
        printf("Yes, Jack is younger than Ellen");
    }else{
        printf("No, Jack is older than Ellen ");
    }
    return 0;
}

Result:

Yes, Jack is younger than Ellen

In the example above, we’ve tried to see if Jack `jackAge` is younger than Ellen `ellenAge` via less-than `<` operator within the condition of the `if` statement. In this example, Jack is younger than Ellen and so the result of this comparison becomes true and the body of `if` statement in this case will run. That’s how we’ve got the result you saw above.

C Greater than or Equal To `>=` Operator:

This operator is the combination of two other operators:

  • Greater operator `>`.
  • Equal operator `==`.

Basically via this operator, we can compare two values (constants, variables or expressions) to see if the value on the left side of the comparison is greater or equal in size compared to the value on the right side.

If the left value was greater or equal compared to the value on the right side of the comparison, then the result will be true, otherwise we will get a false value.

Greater than or Equal To `>=` Operator Example:

#include <stdio.h>

int main() {
    
    int age = 21;

    if (age >= 21){
        printf("This person is allowed to enter to a Bar");
    }else{
        printf("This person can't enter to a Bar");
    }
    return 0;
}

Result:

This person is allowed to enter to a Bar

In the example above, the condition is to check whether the value of the `age` variable is greater or equal to the value 21 within the condition of the `if` statement. Because here, the variable’s value is equal to the value 21, the condition becomes true and so the instructions in the body of the `if` statement will run and so that’s why we got the result you see above on the output.

C Less Than or Equal To `<=` Operator:

This operator is the combination of two other operators:

  • Less-Than operator `<`.
  • Equal operator `==`.

Basically, via this operator, we can compare two values (constants, variables or expressions) to see if the value on the left side of the comparison is lesser or equal in size compared to the value on the right side.

If the left value was lesser or equal compared to the value on the right side of the comparison operator, then the result becomes true, otherwise we will get a false value.

Less Than or Equal To `<=` Operator Example:

#include <stdio.h>

int main() {
    
    int age = 20;

    if (age <= 21){
        printf("This person is not allowed to enter to a Bar");
    }else{
        printf("This person can enter to a Bar");
    }
    return 0;
}

Result:

This person is not allowed to enter to a Bar

In this example, the condition is to check whether the value of the `age` variable is lesser or equal to the value 21 in the `if` statement. Because the variable’s value is equal to 20, then the condition is true and so the instructions in the body of the `if` statement will run and that’s why we got the result you see on the output stream.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies