Logical Operators in C

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
By Prateek Narang
Free
star5
Enrolled: 1000
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
Prateek Narang
Free
5
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Overview

The C language provides a set of 3 operators that can help you when you need to combine the result of two or more logical expressions or conditions or boolean values; Out of the three operators, two of them are binary, and one is a unary operator. The three operators are &&, ||, ! They perform the logical AND, logical OR and logical NOT respectively. These are called as Logical operators in C.

Scope

  • In this article, we will dive deep into Logical operators in C.
  • We will look at the application of logical operators in C.
  • We will understand how different logical operators in C like AND, OR, NOT work.

What Are Logical operators in C?

The C language provides a set of three operators by default; the sort of things you can use just out of the box in any program without having to import any header file. These operators can be used to combine the result of two or more conditions.

The collection of the operators is known as the logical operators. Every programming language has these operators, and they are all quite similar, except for the symbol that is being used to represent them.

Types of Logical operators in C:

I have been mentioning that three operators are collectively called logical operators. So it's now time to unveil the screen and announce those three operators.

Operator NameDescriptionSymbol
logical ANDThis operator combines the result of two inputs and returns true only when both of them evaluate to be true and false if any of them evaluates to be false.&&
logical ORThis operator combines the result of two inputs and returns true when any one of them evaluates to be true and false only if both of them evaluate to be false.||
logical NOTThis is a unary operator and complements the input that has been passed to it. If the input is true the operator returns false and vice versa.!

types of logical operators in c

Logical AND

The logical AND is a binary operator. It takes two inputs and combines them. The result is true only when both the inputs evaluate to be true and false if any of the inputs is false.

If the first input evaluates to be false, then the operator will not even consider checking the next input because the output is going to be false irrespective of the other input.

the truth table for the logical AND operator.

Input 1Input 2Result
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

Code:

Output:

Logical OR

The logical OR is similar to the logical AND operator. This is also a binary operator and needs two operands or inputs to perform the operation. The result is true when one of the two inputs is true, the output of the logical OR operation is false only when both the input values evaluate to be false.

If the first input value to the operator evaluates to be true, then the compiler will skip checking the second input and directly return true as the output. Because irrespective of the second condition, the output of the logical OR operation is going to be true.

The truth table for logical OR operation:

Input 1Input 2Result
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

Code:

Output:

Logical NOT

The logical NOT operator is the only unary operator among the logical operators. This will take one input and return the complement of the input as the output. If the input is true then the output will be false, and if the input is false then the output will be true.

The truth table for logical NOT operation:

Input 1Result
truefalse
falsetrue

Code:

Output:

Shortcircuiting With Logical operators in C

The Logical operators in C perform their operations in a manner that, when properly used by the programmer, can reduce the lines of code and the time complexity of the program; in short, the overall efficiency of the program.

If you wish to improve the efficiency of your program; you need to remember these two points.

  • The logical AND operator does not evaluate the second input when the first one evaluates it to be false.
  • The logical OR operator does not evaluate the second input when the first input evaluates it to be true.

So how can you use these two ideas to improve your program's efficiency?

Let's explain this with a simple program, one you might be more familiar with -- the program to find whether a given year is a leap year or not.

The conditions for a leap year

  • condition1 -- the year is divisible by 4 and not divisible by 100
  • condition2 -- if the year is divisible by both 4 and 100 then the year should also be divisible by 400

If either of these two conditions satisfies, then the given year is a leap year.

Firstly, let me start with the brute force approach.

This is the approach that many of those newbie programmers take when they are asked to write when they are provided with this problem. As you can see, we are checking around 3 conditions to check whether a given year is a leap year or not.

This code is too big, and the time complexity is worse. So let's see the optimal approach for this problem.

The optimal approach:

The optimal approach is built based on some mathematical reasoning that you must be aware of. So, the things that you need to remember are

  • any number divisible by 400 will be divisible by both 4 and 100. So you have to check this condition first, cause if it's divisible by 400 then you don't have to check the other conditions to say it is a leap year.
  • If it's not divisible by 400 then the year can still be a leap year, yeah think about 2020 it is a leap year that is not divisible by 400.
  • So now you have to check whether the given year is divisible by 4 and not divisible 100. To perform this check you can use the logical AND operator.

So let's wrap it up, A given year is a leap year if it satisfies either of these two conditions:

  • Condition 1: Divisible by 4 and not divisible by 100
  • Condition 2: Divisible by 400

So now the problem has been reduced to a very simple level. You can solve this with the help of logical AND, and logical OR operators. You can do that in a single if condition.

Now we can employ the technique of short-circuiting to make it a little more efficient, shall we?

The short-circuiting is mainly dependent on the order in which you place the conditions. There are two ways you can place these two conditions inside the if condition.
They are:

  • approach 1 condition 1 || condition 2
  • approach 2 condition 2 || condition 1

Here if you choose the approach 1 then the compiler will be checking all the three cases only when the given year is divisible by 4 and not by 100, at that time you have no other option except to check whether the given number is divisible by 400 or not.

But, if you choose the approach 2 then the compiler will be checking only one condition if the year is divisible by 400, if it's not divisible by 400 then the compiler will check whether it is divisible by 4 or not, it will check the third condition if this evaluates to be true.

The thing you should think about before choosing one of these approaches is that there are a lot of years divisible by 4 and fewer years divisible by 100 and even fewer years which are divisible by 400.

So if you choose the second approach then you will end up checking all the three conditions for most of the given inputs.

So, the optimal approach to go with is approach 1

The optimal approach is

Conclusion

  • In this article, we have seen the need for the Logical operators in C and what are the three logical operators in C (AND, OR, NOT).
  • We have also seen about all the Logical operators in C individually and saw their truth tables and usecases.
  • We learned a great technique called short-circuiting that can help improve the time complexity of certain programs when used in the best possible way.