- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
Logical Operators in C
Logical operators in C evaluate to either True or False. Logical operators are typically used with Boolean operands.
The logical AND operator (&&) and the logical OR operator (||) are both binary in nature (require two operands). The logical NOT operator (!) is a unary operator.
Since C treats "0" as False and any non-zero number as True, any operand to a logical operand is converted to a Boolean data.
Here is a table showing the logical operators in C −
Operator | Description | Example |
---|---|---|
&& | Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. | (A && B) |
|| | Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true. | (A || B) |
! | Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. | !(A) |
The result of a logical operator follows the principle of Boolean algebra. The logical operators follow the following truth tables.
Logical AND (&&) Operator
The && operator in C acts as the logical AND operator. It has the following truth table −
a | b | a&&b |
---|---|---|
true | true | True |
true | false | False |
false | true | False |
false | false | False |
The above truth table shows that the result of && is True only if both the operands are True.
Logical OR (||) Operator
C uses the double pipe symbol (||) as the logical OR operator. It has the following truth table −
a | b | a||b |
---|---|---|
true | true | True |
true | false | True |
false | true | true |
false | false | false |
The above truth table shows that the result of || operator is True when either of the operands is True, and False if both operands are false.
Logical NOT (!) Operator
The logical NOT ! operator negates the value of a Boolean operand. True becomes False, and False becomes True. Here is its truth table −
A | !a |
---|---|
True | False |
False | True |
Unlike the other two logical operators && and ||, the logical NOT operator ! is a unary operator.
Example 1
The following example shows the usage of logical operators in C −
#include <stdio.h> int main(){ int a = 5; int b = 20; if (a && b){ printf("Line 1 - Condition is true\n" ); } if (a || b){ printf("Line 2 - Condition is true\n" ); } /* lets change the value of a and b */ a = 0; b = 10; if (a && b){ printf("Line 3 - Condition is true\n" ); } else { printf("Line 3 - Condition is not true\n" ); } if (!(a && b)){ printf("Line 4 - Condition is true\n" ); } return 0; }
Output
Run the code and check its output −
Line 1 - Condition is true Line 2 - Condition is true Line 3 - Condition is not true Line 4 - Condition is true
Example 2
In C, a char type is a subset of int type. Hence, logical operators can work with char type too.
#include <stdio.h> int main(){ char a = 'a'; char b = '\0'; // Null character if (a && b){ printf("Line 1 - Condition is true\n" ); } if (a || b){ printf("Line 2 - Condition is true\n" ); } return 0; }
Output
Run the code and check its output −
Line 2 - Condition is true
Logical operators are generally used to build a compound boolean expression. Along with relational operators, logical operators too are used in decision-control and looping statements in C.
Example 3
The following example shows a compound Boolean expression in a C program −
#include <stdio.h> int main(){ int phy = 50; int maths = 60; if (phy < 50 || maths < 50){ printf("Result:Fail"); } else { printf("Result:Pass"); } return 0; }
Output
Result:Pass
Example 4
The similar logic can also be expressed using the && operator as follows −
#include <stdio.h> int main(){ int phy = 50; int maths = 60; if (phy >= 50 && maths >= 50){ printf("Result: Pass"); } else { printf("Result: Fail"); } return 0; }
Output
Run the code and check its output −
Result: Pass
Example 5
The following C code employs the NOT operator in a while loop −
#include <stdio.h> int main(){ int i = 0; while (!(i > 5)){ printf("i = %d\n", i); i++; } return 0; }
Output
In the above code, the while loop continues to iterate till the expression "!(i > 5)" becomes false, which will be when the value of "i" becomes more than 5.
i = 0 i = 1 i = 2 i = 3 i = 4 i = 5
C has bitwise counterparts of the logical operators such as bitwise AND (&), bitwise OR (|), and binary NOT or complement (~) operator.