w3resource

C Relational Operators with Examples

Relational Operators in C

Overview

Relational operators are used to compare two operands, and depending on their relation, certain decisions are taken.

Relational operators are essential for making decisions in C programs. They compare values and return a Boolean result, which helps control the flow of the program in conditional statements. The result of the comparison is either true (1) or false (0). These operators are essential in decision-making structures like if, while, and for loops.

The main Relational operators in C are: == (Equal to), != (Not equal to), > (Greater than), < (Less than), >= (Greater than or equal to) and <= (Less than or equal to) Operator.

Key Topics:

== (Equal to) Operator with Example

Compares two values to check if they are equal.

  • When to Use: Use this operator when you want to verify that two variables or values are the same. It is commonly used in conditional statements like if or loops to check for equality.
  • Why to Use: To control the flow of the program based on whether two values are exactly equal.
  • Example: Using == (Equal to) Operator

    This example shows the use of the == operator to compare if two values are the same.

    Code:

    #include <stdio.h>
    int main() {
        int a = 5, b = 5;   
        // Check if a is equal to b
        if (a == b) {
            printf("a is equal to b\n");
        }
        else {
            printf("a is not equal to b\n");
        }    
        return 0;
    }
    

    Output:

    a is equal to b
    

    Explanation:

    • This program checks if a is equal to b using the == operator.
    • Since a and b are both 5, the condition a == b is true, and the output will be "a is equal to b".

    != (Not equal to) Operator with Example

    Compares two values to check if they are not equal.

    • When to Use: Use this operator when you need to determine if two values are different.
    • Why to Use: Helps to execute code based on whether two variables hold different values. Useful in decision-making statements where the difference matters.

    Example: Using != (Not equal to) Operator

    This example demonstrates the use of != to check if two values are different.

    Code:

    #include <stdio.h>
    int main() {
        int a = 10, b = 5;
        
        // Check if a is not equal to b
        if (a != b) {
            printf("a is not equal to b\n");
        }
        else {
            printf("a is equal to b\n");
        }
        
        return 0;
    }
    

    Output:

    a is not equal to b
    

    Explanation:

    • The != operator checks if a and b are not equal.
    • Since a is 10 and b is 5, the condition is true, so the output will be "a is not equal to b".

    > (Greater than) Operator with Example

    Checks if the left operand is greater than the right operand.

    • When to Use: Use this operator when comparing two values and you want to ensure that the first one is strictly larger than the second.
    • Why to Use: Useful for conditions where an action needs to occur only if one value exceeds another, such as in loops, sorting algorithms, or decision-making.

    Example: Using > (Greater than) Operator

    This example shows the use of the > operator to compare if one value is larger than another.

    Code:

    #include <stdio.h>
    int main() {
        int a = 7, b = 3;    
        // Check if a is greater than b
        if (a > b) {
            printf("a is greater than b\n");
        }
        else {
            printf("a is not greater than b\n");
        }    
        return 0;
    }
    

    Output:

    a is greater than b
    

    Explanation:

    • The > operator checks if a is greater than b.
    • Since a is 7 and b is 3, the condition a > b is true, so the output will be "a is greater than b".

    < (Less than) Operator with Example

    Checks if the left operand is smaller than the right operand.

    • When to Use: Use this operator when you want to verify that one value is strictly smaller than another.
    • Why to Use: Often used in loops, iterations, or conditions where an operation should only be executed if a value is below a certain threshold.

    Example : Using < (Less than) Operator

    This example demonstrates the use of the < operator to compare if one value is smaller than another.

    Code:

    #include <stdio.h>
    int main() {
        int a = 2, b = 8;    
        // Check if a is less than b
        if (a < b) {
            printf("a is less than b\n");
        }
        else {
            printf("a is not less than b\n");
        }
            return 0;
    }
    

    Output:

    a is less than b
    

    Explanation:

    • The < operator checks if a is less than b.
    • Since a is 2 and b is 8, the condition a < b is true, so the output will be "a is less than b".

    >= (Greater than or equal to) Operator with Example

    Checks if the left operand is greater than or equal to the right operand.

    • When to Use: Use this operator when comparing two values, and the condition should pass if the left operand is either larger than or equal to the right operand.
    • Why to Use: Useful when you want to include equality in the comparison and when a value should satisfy a condition even if it's exactly equal to the second value.

    Example: Using >= (Greater than or equal to) Operator

    This example shows how the >= operator can be used to compare values for greater than or equal to.

    Code:

    #include <stdio.h>
    
    int main() {
        int a = 5, b = 5;
        
        // Check if a is greater than or equal to b
        if (a >= b) {
            printf("a is greater than or equal to b\n");
        }
        else {
            printf("a is less than b\n");
        }
        
        return 0;
    }
    

    Output:

    a is greater than or equal to b
    

    Explanation:

    • The >= operator checks if a is greater than or equal to b.
    • Since a and b are both 5, the condition a >= b is true, and the output will be "a is greater than or equal to b".

    <= (Less than or equal to) Operator with Example

    Checks if the left operand is smaller than or equal to the right operand.

    • When to Use: Use this operator when comparing two values, and the condition should be true if the first value is smaller or equal to the second.
    • Why to Use: Helpful in conditions where the value needs to be within a certain range or should meet a limit, including equality with the lower bound.

    Example : Using <= (Less than or equal to) Operator

    This example shows the use of the <= operator to check for less than or equal to conditions.

    Code:

    #include <stdio.h>
    int main() {
        int a = 3, b = 7;
        // Check if a is less than or equal to b
        if (a <= b) {
            printf("a is less than or equal to b\n");
        }
        else {
            printf("a is greater than b\n");
        }   
        return 0;
    }
    

    Output:

    a is less than or equal to b
    

    Explanation:

    • The <= operator checks if a is less than or equal to b.
    • Since a is 3 and b is 7, the condition a <= b is true, so the output will be "a is less than or equal to b".
    

    Become a Patron!

    Follow us on Facebook and Twitter for latest update.

    It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

    https://www.w3resource.com/c-programming/c-relational-operators.php