Logical Operators in C Programming

Logical Operators in C Programming

13 Jun 2024
Beginner
108 Views
11 min read
Learn via Video Course & by Doing Hands-on Labs

C Programming For Beginners Free Course

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

Q1. What are logical operators in C?

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

Q2. What is the symbol of a logical operator?

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

Q3. Can logical operators be chained together?

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

Q4. Why do we use the Logical operators in C?

We use logical operators in C to combine multiple conditions in a program and control the program's flow.
Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
ASP.NET Core Certification Training Jul 17 MON, WED, FRI
Filling Fast
07:00AM to 08:30AM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification Training Jul 17 MON, WED, FRI
Filling Fast
07:00AM to 08:30AM (IST)
Get Details
Angular Certification Course Jul 20 SAT, SUN
Filling Fast
06:00PM to 08:00PM (IST)
Get Details
Generative AI For Software Developers Jul 20 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Azure Master Class Jul 20 SAT, SUN
Filling Fast
03:00PM to 05:00PM (IST)
Get Details
ASP.NET Core Certification Training Jul 28 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
Software Architecture and Design Training Jul 28 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
.NET Solution Architect Certification Training Jul 28 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
Azure Developer Certification Training Jul 28 SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification Training Jul 28 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
Data Structures and Algorithms Training with C# Jul 28 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Angular Certification Course Aug 11 SAT, SUN
Filling Fast
09:30AM to 11:30AM (IST)
Get Details
ASP.NET Core Project Aug 24 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details

Can't find convenient schedule? Let us know

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.

Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Accept cookies & close this