May 05, 2018

Relational Operators in C


   Relational operators are used to compare values of two expression. Expressions that contain relational operators are called relational expressions.

Relational operators return true or false value, depending on whether the conditional relationship between the two operands holds or not. If the relation is true then the result of relational expression is 1 and if the relation is false then the result of relational expression is 0.

For example, to test the expression, if a is greater than b, the relational operator > is used as a > b. This expression will return true value if a is greater than b; otherwise the value of the expression will be false.



The relational operators are:


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


Let us take two variables a=10 and b=8, and form simple relational expressions with them:


Expression Relation Result
a < b False 0
a <= b False 0
a > b True 1
a >= b True 1
a == b False 0
a != b True 1


Always remember in C a non-zero value means true and 0 means false.

The following program demonstrates relational operators.

#include <stdio.h>

int main(void)
{
    int a = 10, b = 8;

    printf("Value of a = %d\n", a);
    printf("Value of b = %d\n\n", b);

    /* a is greater than b */
    printf("a > b : %d\n", a > b);

    /* a is greater than or equal to b */
    printf("a >= b : %d\n", a >= b);

    /* a is less than b */
    printf("a < b : %d\n", a < b);

    /* a is less than or equal to b */
    printf("a <= b : %d\n", a <= b);

    /* a is equal to b */
    printf("a == b : %d\n", a == b);

    /* a is not equal to b */
    printf("a != b : %d\n", a != b);

    return 0;
}


Output:

Value of a = 10
Value of b = 8

a > b : 1
a >= b : 1
a < b : 0
a <= b : 0
a == b : 0
a != b : 1


The relational operators are generally used in if...else construct and loops, which we will learn in upcoming tutorials. In our next program we will use the if statement to illustrate the use of relational operators. The if control statement evaluates an expression, and if this expression is true(non zero) then the next statement is executed, otherwise the next statement is skipped.

The following program demonstrates relational operators.

#include <stdio.h>

int main(void)
{
    int a, b;

    printf("\nEnter value for a : ");
    scanf("%d", &a);
    printf("\nEnter value for b : ");
    scanf("%d", &b);

    printf("\n\n");
   
    if(a > b)
        printf("%d is greater than %d\n", a,b);

    if(a >= b)
        printf("%d is greater than or equal to %d\n", a,b);

    if(a < b)
        printf("%d is less than %d\n", a,b);

    if(a <= b)
        printf("%d is less than or equal to %d\n", a,b);

    if(a == b)
        printf("%d is equal to %d\n", a,b);

    if(a != b)
        printf("%d is not equal to %d\n", a,b);

    return 0;
}


Output:

Enter value for a : 10

Enter value for b : 8

10 is greater than 8
10 is greater than or equal to 8
10 is not equal to 8


Don't confuse assignment operator (=) with equal to operator (==). The assignment operator (=) is used to assign the value of variable or expression, while equal to (==) is a relational operator used for comparison (to compare value of both left and right side operands).



next    Logical Operators in C
top    Index
prev    Increment and Decrement Operators in C






No comments:

Post a Comment