Logical Operators in C
The C language provides a set of 3 operators that can help you when you need to combine the result of two or more logical expressions or conditions or boolean values; Out of the three operators, two of them are binary, and one is a unary operator. The three operators are &&, ||, ! They perform the logical AND, logical OR and logical NOT respectively. These are called as Logical operators in C.
Types of Logical Operators in C
The following are three types of logical operators in C :
Operator Name | Description | Symbol |
---|---|---|
logical AND | This operator combines the result of two inputs and returns true only when both of them evaluate to be true and false if any of them evaluates to be false. | && |
logical OR | This operator combines the result of two inputs and returns true when any one of them evaluates to be true and false only if both of them evaluate to be false. | || |
logical NOT | This is a unary operator and complements the input that has been passed to it. If the input is true the operator returns false and vice versa. | ! |
Logical (AND) && Operator in C
The logical AND is a binary operator. It takes two inputs and combines them. The result is true only when both the inputs are evaluated to be true and false if any of the inputs is false.
If the first input evaluates to be false, then the operator will not even consider checking the next input because the output is going to be false irrespective of the other input.
The truth table for the logical AND operator.
Input 1 | Input 2 | Result |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
Use Cases:
- Conditional Statements: Logical AND (&&) is commonly used in conditional statements to express compound conditions. For example:
- Guard Clauses: It's frequently used in guard clauses to perform quick checks and avoid unnecessary execution of subsequent code:
Precedence and Associativity:
The logical AND operator (&&) has higher precedence than the logical OR operator (||) and the assignment operator (=). It is left-associative, meaning it's evaluated from left to right.
Type Conversion:
The operands of the logical AND operator are subjected to type conversion to bool if they are not already of type bool. Any non-zero value is considered true, and zero is considered false.
Code:
Output:
Logical OR in C Programming
The logical OR is similar to the logical AND operator. This is also a binary operator and needs two operands or inputs to perform the operation. The result is true when one of the two inputs is true, the output of the logical OR operation is false only when both the input values evaluate to be false.
If the first input value to the operator evaluates to be true, then the compiler will skip checking the second input and directly return true as the output. Because irrespective of the second condition, the output of the logical OR operation is going to be true.
The truth table for logical OR operation:
Input 1 | Input 2 | Result |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
Use Cases:
- Conditional Statements: Logical OR (||) in C programming is commonly used in conditional statements to express compound conditions where either one or both conditions need to be true. For example:
- Fallbacks or Default Values: It's frequently used to provide fallbacks or default values:
In the above example, value will be assigned userInput if it's not zero (interpreted as true), otherwise, defaultValue will be used.
Precedence and Associativity:
The logical OR operator (||) has lower precedence than the logical AND operator (&&) but higher precedence than the assignment operator (=). It is left-associative, meaning it's evaluated from left to right.
Type Conversion:
Similar to the logical AND operator, the operands of the logical OR operator are subjected to type conversion to bool if they are not already of type bool. Any non-zero value is considered true, and zero is considered false.
Code:
Output:
Logical NOT Operator in C
The logical NOT operator is unary operator which takes one input and return the complement of the input as the output. If the given input is true then the output will be false and vice versa.
The truth table for logical NOT operation:
Input 1 | Result |
---|---|
true | false |
false | true |
Precedence and Associativity:
The logical NOT operator (!) has higher precedence than both the logical AND operator (&&) and the logical OR operator (||). It is right-associative, meaning it's evaluated from right to left.
Code:
Output:
Short Circuiting With Logical operators in C
When the result can be determined by evaluating the preceding Logical expression without evaluating the further operands, it is known as short-circuiting.
The following two points need to keep in mind:
- The logical AND operator does not evaluate the second input when the first one evaluates it to be false.
- The logical OR operator does not evaluate the second input when the first input evaluates it to be true.
Short Circuit with Logical AND Operator
The logical AND operator returns true if and only if all operands evaluate to true. If the first operand is false, then the further operands will not be evaluated. This is because even if the further operands evaluate to true, the entire condition will still return false.
Example:
Short Circuit with Logical OR Operator
The logical OR operator returns true if at least one operand evaluates to true. If the first operand is true, then the further operands will not be evaluated. This is because even if the further operands evaluate to false, the entire condition will still return true.
Example:
Conclusion
- C provides three logical operators: && (logical AND), || (logical OR), and ! (logical NOT).
- Logical AND: Returns true only if both operands are true.
- Logical OR: Returns true if at least one operand is true.
- Logical NOT: Returns the opposite boolean value of the operand.
- We have seen each of the logical operators examples above.
- Logical AND Short-circuiting: Skips evaluating further operands if the first operand is false.
- Logical OR Short-circuiting: Skips evaluating further operands if the first operand is true.
- Short-circuiting improves performance by avoiding unnecessary evaluations.