Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
Logical Operators in C Programming

Logical Operators in C Programming

13 Jun 2024
Beginner
1.36K Views
11 min read
Learn with an interactive course and practical hands-on labs

C Programming Free Course with Certificate: Learn C In 21 Days

C Logical Operators

Logical Operators in C use specific expressions as input arguments for performing logical operations. They output a boolean value that can either be true or false. Consequently, such operators are advantageous since they allow one to make choices within their code by executing different sets of instructions depending on some conditions.

Let's look at C logical operators in this C Tutorial, to answer what these mean, and understand how they work through Logical Operators Examples. Explore other concepts related to C Programming by enrolling in our C Certification Training right now!

Before tuning in to Logical Operators, we recommend you first go through a basic overview of what Operators in C Programming are.

Why Use Logical Operators in C Programming?

Logical Operators play a vital role in controlling the flow of a program. They allow you to evaluate multiple conditions and make decisions based on the results. This is particularly useful in if, while, and for statements, where you need to perform actions based on whether certain conditions are met.

Types Of Logical Operators In C

C offers three primary logical operators: AND, OR, and NOT. Each has a unique function and usage. Let's see all of these logical operators examples in C Online Compiler.

1. Logical AND Operator ( && )

The logical AND operator (&&) returns true if both operands are true. If any of the operands are false, the operator will return false. It is frequently utilized when many requirements must be fulfilled for an activity to take place.

ABA && B
111
100
010
000

Syntax:

condition1 && condition2

Example of Logical AND Operator in C:

#include <stdio.h>

int main() {
    int a = 5;
    int b = 10;

    if (a > 0 && b > 0) {
        printf("Both a and b are positive numbers.\n");
    }

    return 0;
}

The method will return TRUE only if both condition1, as well as condition2, are TRUE. In this example, it is positive for both a and b, and the message gets printed.

Output

Both a and b are positive numbers.

2. Logical OR Operator ( || )

The (||) operator gives a true value when one of condition 1 or condition 2 is true or both are true. It only returns false if both operands are false. This operator is helpful when one wishes to implement any action upon meeting any condition of interest.
ABA || B
111
101
011
000

Syntax:

condition1 || condition2

Example of Logical OR Operator in C:

#include <stdio.h>

int main() {
    int a = -5;
    int b = 10;

    if (a > 0 || b > 0) {
        printf("At least one of a or b is a positive number.\n");
    }

    return 0;
}

The || operator returns true if at least one of condition 1 or condition 2 is true. Here, even though a is not positive, b is, making the overall condition true.

Output

At least one of a or b is a positive number.

3. Logical NOT Operator ( ! )

The NOT operator reverses the value of the operand. So that when an operand is TRUE, FALSE is returned and vice versa if FALSE then TRUE will be returned. It's often used to toggle Boolean values or to simplify complex conditions.
A!A
01
10

Syntax:

!condition

Example of Logical NOT Operator in C:

#include <stdio.h>

int main() {
    int a = 5;

    if (! (a < 0)) {
        printf("a is not a negative number.\n");
    }

    return 0;
}

The ! operator negates the condition. If the condition is true, it returns false, and vice versa. In this example, a is not negative, so !(a < 0) returns true.

Output

a is not a negative number.
Read More: Beginner's Guide to C Programming

Functions Of Logical Operators In C

Logical operators in C serve several key functions:
  • They help control a program's flow by allowing you to run specific code blocks under certain conditions.
  • Logical operators make the process of decision-making easier with if, while, and for statements.
  • They allow you to combine multiple conditions, making your code more concise and readable.

Example Illustrating Logical Operators In C

This is an example program in C Compiler that uses all three logical operators:
#include <stdio.h>

int main() {
    int age = 25;
    int experience = 5;
    int criminalRecord = 0;
    int education = 1; // 1 means the applicant has a degree, 0 means no degree

    // Use all three logical operators: &&, ||, and !
    if ((age > 18 && experience >= 5) || (education && !criminalRecord)) {
        printf("You are eligible for the job.\n");
    } else {
        printf("You are not eligible for the job.\n");
    }

    return 0;
}

The purpose of the program provided is to determine the eligibility of the candidate based on factors like age, which must be more than eighteen years and five years of experience or have a degree and no criminal record. If these conditions are satisfied, then the candidate will secure the job.

Output

You are eligible for the job.

Short-Circuiting In The Case Of Logical Operators In C

Short-circuiting is a significant feature of logical operators. It means the evaluation stops as soon as the result is determined:
  1. Logical AND ( && )-When the first condition is checked and it is False, there is no need to further check the second condition. The result will anyway be false if even one of the conditions is false.
  2. Logical OR ( || )-When the first condition is checked and it is found to be true the second condition will not be checked since anyway at least one is true. The result will also be true.

C Logical Operators Advantages

  • They make complex conditions easy to write and read.
  • Short-circuiting can save computational resources.
  • They allow for robust decision-making in programs.

C Logical Operators Disadvantages

  • When conditions are complex, debugging can become challenging.
  • Improper use can lead to logical errors in the program.
  • Although generally efficient, overly complex conditions can affect performance.
Conclusion
In this article, we talked about different types of logical operators in C with their proper syntax and examples. Understanding C logical operators is essential for any programmer. If you want to expand your knowledge in C Programming, consider enrolling in our C Certification Course right now to upskill yourself.
Similar Articles:
Arithmetic Operators in C Programming
Relational Operators in C Programming
C Programming Assignment Operators
Bitwise Operators in C: AND, OR, XOR, Shift & Complement
Also Read: Top 50 Mostly Asked C Interview Questions and Answers

FAQs

Logical Operators in C are used to perform logical operations on expressions that return Boolean values as a result.

The symbols of logical operators in C are:
  1. '&&' for the Logical AND operator
  2. '||' for the Logical OR operator
  3. '!' for the Logical NOT operator

Yes, logical operators can be chained together in order to combine multiple conditions.

We use logical operators in C to combine multiple conditions in a program and control the program's flow.
Share Article
About Author
Sakshi Dhameja (Author and Mentor)

She is passionate about different technologies like JavaScript, React, HTML, CSS, Node.js etc. and likes to share knowledge with the developer community. She holds strong learning skills in keeping herself updated with the changing technologies in her area as well as other technologies like Core Java, Python and Cloud.

Accept cookies & close this