Logical operators in C language ( &&, ||, and ! ) usage and Examples

Spread the love
Logical-Operators-in-C-Programming-Langauge-with-Example-programs

Introduction:

In the previous blog post, we discussed Relational operators in C Language. In today’s article, we are going to look at the Logical Operators in C programming.

Logical Operators in C Language:

The Relational Operators are useful to find the relations between the two expressions, numbers, etc.

But Sometimes there will be the need to combine or check multiple relations at the same time.

For example, You want to check if the given number ( x) is between 10 and 20.

None of the relational operators alone can achieve the above requirement. We need to combine two relation operators like

x > 10 and x < 20

To Combine the two relational operators, We use the Logical operators ( Except Not operator).

Here we want both relations x>20 and x<20 to be True. In such cases, we use Logical AND Operator.

C language supports three logical operators, They are

  1. Logical AND Operator ( && )
  2. Logical OR Operator ( || )
  3. Logical NOT Operator ( ! )

📢 C Langauge Logical operators return 0 for False and 1 for True. The Operand for Logical operator may be Constant, Variable, or Expression.

Logical Operators output is boolean ( Either True or False )

All Non-Zero values are considered as the True values in Logical Operations. Ex: -20, 50, 33, etc

Logical AND Operator ( && ) in C Language:

The Logical AND Operator works on two operands. This means Logical AND Operator needs two input variables. Depending upon inputs, it will provide the output.

The Logical AND Operator is denoted by && symbol in the C programming.

Syntax of Logical Operator:

If both operands ( Input values ) are Non-zero then only Logical AND output will be TRUE. In all other cases, it will return False.

Logical AND Truth table:

Here is the truth table of the Logical AND Operator

ABA && B
000
010
100
111

Example Program to Understand Logical AND && operator :

We are going to print the above truth table using the C language. As discussed Logical AND operator output will be True or One, Only if the two input values are true or non-zero.

Program Output:

Logical OR Operator ( || ) in C Langauge with Examples:

The Logical OR Operator also operates on the Two Operands or Input values.

Logical OR Operator is denoted by the || symbol in the c programming language.

Syntax of Logic OR Operator :

As the name suggests, If any of the input operands are non-zero or true, then the Logical OR output will be True.

In other words, If both Operands are zero or False, Then only the output of the Logical OR will be False ( Zero )

Truth Table of Logical OR Operator:

ABA || B
000
011
101
111

Example Program to understand Logical OR ( || ) Operator:

Here is a program to display the Logical OR output for various input values.

In order to get the logical OR output as True, One of the input values must need to be non-zero or True.

Program Output:

The input values need not be the Zeros (0) and Ones(1). We can apply the Logical Operators on any values.

Logical OR Example on non-zero values:

Program Output:

Logical Operators treat non-zero values as True values. Even the Negative Integer values are also considered True. Only the value Zero is False.

📢 All Non-Zero values including Negative values are considered as True

Logical NOT ( ! ) Operator in C Programming:

The Logical NOT Operator works on one Operand ( One Input value)

Logical NOT Operator gives us the Negation of Input Value.

If the input Operand value is Non-Zero, Then Logical NOT Operator output will be Zero.

Similarly, If the Input Operand value is Zero, Then the output will be True.

Logical NOT Operator is denoted by an Exclamation mark ( ! )

Truth Table of Logical NOT:

A!A
10
01

As you can see, The NOT operator is giving the opposite value as the output.

Program to Understand the Logical NOT operator:

Program Output:

As we discussed earlier, All Non-Zero values are True, So applying the Logical NOT operator on all non-zero values results into the False

Program Logical NOT on Non-zero Values:

Output:

Conclusion:

In this article, We have looked at the Logica Operators in C Language and different types of Logical operators with example programs.

Tutorials Index:

Related Articles:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

6 Responses

  1. November 29, 2018

    […] Logical Operators […]

  2. June 20, 2021

    […] Logical Operators […]

  3. July 25, 2021

    […] our previous article, We discussed the Logical Operators in C, In today’s article, We will look at the Conditional Operator in C Langauge. Conditional […]

  4. July 25, 2021

    […] our previous articles, We have discussed the Logical Operators and Conditional operators in C Language. In today’s article, We are going to look at the […]

  5. July 27, 2021

    […] ➡️ Learn More about Logical Operators Here – Logical operators in C language ( &&, ||, and ! ) usage and Examples (sillycodes.com) […]

  6. August 7, 2021

    […]  📢 Learn More about Logical Operators Here – Logical operators in C language ( &&, ||, and ! ) usage and Examples (sillycodes.com) […]

Leave a Reply