C Logical Operators Tutorial

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

Note: Before reading this section, you should already be familiar with operators in general and the comparison-operators.

What is Logical Operators in C?

Sometimes we want to run multiple comparisons at the same time and only if the results of comparisons altogether were true, then to run a set of instructions, otherwise we want to skip over those instructions, for example! Or we might want to run multiple comparisons and only if ONE of those comparisons resulted true, then run a set of instructions.

This is where we can use the `logical-operators`!

List of Logical operators in C

Here’s an introductory list of Logical-operators:

Operator Symbol
AND Operator &&
OR Operator ||
Reverse the Result (Not) Operator !

C AND `&&` Operator:

Using this operator, we can put together multiple comparison operations and check to see whether the result of the entire comparisons is true. If all of them resulted true, the end result will be true as well, otherwise even if one comparison resulted false, the end result becomes false as well.

C AND `&&` Operator Example:

#include <stdio.h>

int main() {

    int x = 432;

    if (x >= 100 && x<1000 ){
        printf("The number of digits in this number is 3");
    }
    return 0;
}

Result:

The number of digits in this number is 3

In the example above, we’ve run multiple comparisons in the `if` statement via the `&&` operator.

First, we’ve checked to see if the value assigned to the `x` variable is larger than 100, then followed by this comparison we set `&&` operator to add another comparison and this time we’ve checked to see if the value of the `x` variable is lesser than 1000.

Now only if the two comparisons result true; in that case, we can say the end result in the `if` statement is true as well and so the body of the if-statement will run.

Note: as you can see in the example above, the use of `&&` operator comes between each condition.

C OR `||` Operator:

Using this operator, we can put together multiple comparisons and check to see whether the result of only one comparison is true. If one of them resulted true, the end result will be true as well, otherwise if all comparisons resulted false, the end result becomes false as well.

So remember, the `||` operator only cares about one boolean expression to result true. This means if you have like 11 comparisons going on and only one of them resulted true, the end result for this operator will be true as well, even though the other 10 expressions resulted false.

C OR `||` Operator Example:

#include <stdio.h>

int main() {

    int x = 432;

    if (x >= 1000 || x<200 ){
        printf("The number is larger than 1000 or less than 200");
    } else{
        printf("The number neither larger than 1000 nor less than 200");
    }

    return 0;
}

Result:

The number neither larger than 1000 nor less than 200

In the example above, we’ve run multiple comparisons in the `if` statement with the help of the `||` operator.

First, we’ve checked to see if the value assigned to the `x` variable is greater than 1000, then followed by this comparison we set `||` operator to add another comparison and this time we’ve checked to see if the value of the `x` variable is lesser than 200.

Now only if ONE of the two comparisons results true, in that case, we can say the end result in the `if` statement is true as well and so the body of this statement will run.

But as you can see, the end result of the comparisons in the `if` statement is false because the value of `x` variable is neither greater than 1000 nor less than 200. So the body of `else` section ran instead.

Not Operator `!` (AKA Reverse the Result Operator):

The not-operator is used to reverse the result of a boolean expression!

For example, if we use the `==` operator to see if two values are equal (let’s two variables that both have the value 10 as their values), if the result of the comparison becomes true, using this `Not-Operator` this result will be reversed and hence the value false will return instead.

Also, if a boolean expression resulted false, we can use this not-operator to reverse the result and return the value true as a result.

Note: we put the not-operator on the right side of a boolean expression and usually that expression is put inside a pair of parentheses.

For example:

!(boolean-expression)

Now whatever the result of the expression is, using this operator, we will get the reverse of that result.

C Reverse the Result Operator Example:

#include <stdio.h>

int main() {

    int x = 432;

    if (!(x>1000) ){
        printf("x is not larger than 1000");
    }
    return 0;
}

Result:

X is not larger than 1000

We know that the value of `x` variable is not greater than 1000 so the result of x> 1000 should be false. But because we reversed the result via `!` operator, the end result will be true and the body of the `if` statement ran because of this.

Note: You’ll see more use cases for the `!` operator in the `while` statement and working with files sections.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies