21
NovLogical Operators in C Programming
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.
A | B | A && B |
1 | 1 | 1 |
1 | 0 | 0 |
0 | 1 | 0 |
0 | 0 | 0 |
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 ( || )
A | B | A || B |
1 | 1 | 1 |
1 | 0 | 1 |
0 | 1 | 1 |
0 | 0 | 0 |
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 ( ! )
A | !A |
0 | 1 |
1 | 0 |
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
- 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
#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
- 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.
- 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
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
- '&&' for the Logical AND operator
- '||' for the Logical OR operator
- '!' for the Logical NOT operator