Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

Logical Operators in C

We use logical operators to perform various logical operations on any set of given expressions. The logical operators in C are used for combining multiple constraints/ conditions or for complementing the evaluation of any original condition that is under consideration.

In this article, we will discuss the Logical Operators in C according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to know more.

Table of Contents

Types of Logical Operators in C

We have three major logical operators in the C language:

  • Logical NOT (!)
  • Logical OR (||)
  • Logical AND (&&)

Functions of Logical Operators in C

Logical NOT (!) Operator

This type of operator returns true whenever the conditions that are under condition are not at all satisfied. In any other case, it is bound to return false. For instance, the !p will return true if p is false, meaning, when p = 0.

For example

We can write the statement “A student can enroll in my program, if they are not enrolled yet” programmatically as –

if(!enrolled)

{

// Enroll the student

}

Logical OR (||) Operator

This type of operator returns true even when both or even one of the conditions that are under consideration are satisfied. In any other case, it is bound to return false. For instance, the p || q will return true when both or one of p and q are true (non-zero). It also returns to be true when p and q are true.

For example

We can write the statement “It is a holiday, if it is Sunday or Saturday” programmatically as –

if(today == Sunday || today == Saturday)

{

// It is a holiday

}

Note – Don’t confuse bitwise OR | with logical OR ||.

Logical AND (&&) Operator

This type of operator returns true when both the conditions that are under consideration happen to be satisfied. In any other case, it is bound to return false. For instance, the p && q will return true when both- p and q are true (non-zero).

For example

We can write the statement, “He is a handsome guy” programmatically as –

if (skill == ‘C’ && experience >= 2)

{

//He is a handsome guy

}

Note – Do not confuse the bitwise AND & with the logical AND &&.

Examples of Logical Operators in C

Let us take a look at the following example to understand more about all the logical operators in C −

#include <stdio.h>

main() {

int p = 5;

int q = 20;

int r ;

if ( p || q ) {

printf(“Line 5 – Condition is true\n” );

}

if ( p && q ) {

printf(“Line 6 – Condition is true\n” );

}

/* let’s change the value of p and q */

p = 0;

q = 10;

if ( !(p && q) ) {

printf(“Line 7 – Condition is true\n” );

}

if ( p && q ) {

printf(“Line 8 – Condition is true\n” );

} else {

printf(“Line 8 – Condition is not true\n” );

}

}

When we compile the program mentioned above and execute it, we get the following result −

Line 5 – Condition is true

Line 6 – Condition is true

Line 7 – Condition is true

Line 8 – Condition is not true

Here is another example program for the logical operators in C

#include <stdio.h>

int main()

{

int a=40,b=20;

int c=20,d=30;

if (a>b && a !=0)

{

printf(“&& Operator : Both the conditions are true\n”);

}

if (c>d || d!=20)

{

printf(“|| Operator : Only one of the conditions is true\n”);

}

if (!(a>b && a !=0))

{

printf(“! Operator : Both the conditions are true\n”);

}

else

{

printf(“! Operator : Both the conditions are true. ” \

“But, the status is inverted to be false\n”);

}

}

The Output obtained in this case will be:

&& Operator : Both the conditions are true

|| Operator : Only one of the conditions is true

! Operator : Both the conditions are true. But, the status is inverted to be false

  • In the program mentioned above, the operators (||, ! and &&) help us perform various logical operations on the expressions given above.
  • When the conditions (a>b && a!=0) are true, the true (1) is returned. Then this value is inverted by the “!” operator.
  • Thus, the “! (a>b and a! =0)” returns as false (0).
  • ! Operator – This operator is used to reverse the available state of the given operand.
  • || Operator – The “if clause” easily becomes true whenever both or any one of the conditions (c>d || d!=20) are true. It will become false if one of the conditions is true.
  • && operator – The “if clause” only becomes true when both the conditions (a>b and a! =0) are true. Or else, it becomes false.

Short-Circuiting in the case of Logical Operators in C

  • In the case of logical AND (&&), the second operand does not get evaluated when the first operand is false.

For example, the program below doesn’t print “Chocolate” because the first operand of logical AND is false itself.

#include <stdbool.h>

#include <stdio.h>

int main()

{

int p = 10, q = 4;

bool res = ((p == q) && printf(“Chocolate”));

return 0;

}

The output here would be: No Output

The program mentioned below, on the other hand, prints “Chocolate” because the first operand of logical AND in this case is true.

#include <stdbool.h>

#include <stdio.h>

int main()

{

int p = 10, q = 4;

bool res = ((p != q) && printf(“Chocolate”));

return 0;

}

The output here would be: Chocolate

  • In the case of a logical OR, the second operand does not get evaluated if the first operand is true.

For example, the program mentioned below doesn’t print “Chocolate” because the first operand of the logical OR is true itself.

#include <stdbool.h>

#include <stdio.h>

int main()

{

int p = 10, q = 4;

bool res = ((p != q) || printf(“Chocolate”));

return 0;

}

The output here would be: No Output

But the program mentioned below prints “Chocolate” because the first operand of the logical OR is false.

#include <stdbool.h>

#include <stdio.h>

int main()

{

int p = 10, q = 4;

bool res = ((p == q) || printf(“Chocolate”));

return 0;

}

The output here would be: Chocolate

Practice Problems on Logical Operators in C

1. When we use the logical operator AND with two of the variables, how many of these must be true to generate an outcome of the expression that is true as well?

A. Any variable

B. The second variable only

C. The first variable only

D. Both of them

Answer – D) Both of them

2. When we use the logical operator OR with two of the variables, how many of these must be true to generate an outcome of the expression that is true as well?

A. Any variable

B. The second variable only

C. The first variable only

D. Both of them

Answer – A) Any variable


FAQs

Q1

Why do we use the Logical operators in C?

We use the logical operators for performing logical operations of the given relational expressions or the variables. The logical operators in C are used for combining multiple constraints/ conditions or for complementing the evaluation of any original condition that is under consideration.

Q2

What are the types of logical operators in C, and what are the uses of each of these?

We have three major logical operators in the C language – Logical NOT (!), Logical OR (||), and Logical AND (&&). The Logical NOT (!) Operator returns true whenever the conditions that are under condition are not at all satisfied. The Logical OR (||) Operators return true even when both or even one of the conditions that are under consideration are satisfied. The Logical AND (&&) Operator returns true when both the conditions that are under consideration happen to be satisfied.

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility CriteriaGATE 2023GATE Admit CardGATE Syllabus for CSE (Computer Science Engineering)GATE CSE NotesGATE CSE Question Paper, and more.

Also Explore,

Comments

Leave a Comment

Your Mobile number and Email id will not be published.

*

*