- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- 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 - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Logical Operators
![]() Share with a Friend |
C Programming - C Logical Operators
C Logical Operators
Logical operators in C are used to perform logical operations, typically in decision-making and control flow statements. They evaluate expressions and return a boolean result: 1 (true) or 0 (false). Logical operators often work in conjunction with relational operators to build complex conditions.
List of Logical Operators
| Operator | Description | Syntax | Example | Result |
|---|---|---|---|---|
|
&& |
Logical AND |
expr1 && expr2 |
a > 5 && b < 10 |
True if both are true |
|
` |
` |
Logical OR |
`expr1 |
|
|
! |
Logical NOT |
!expr |
!(a > 5) |
True if the condition is false |
Key Characteristics
- Logical AND (&&):
- Evaluates to true (1) only if both operands are true.
C
int a = 10, b = 20;
if (a > 5 && b < 30) {
printf("Condition is True\n");
} else {
printf("Condition is False\n");
}
// Output: Condition is True
- Logical OR (||):
- Evaluates to true (1) if at least one operand is true.
C
int a = 10, b = 20;
if (a > 5 || b > 50) {
printf("Condition is True\n");
} else {
printf("Condition is False\n");
}
// Output: Condition is True
- Logical NOT (!):
- Reverses the logical state of its operand.
- If the condition is true, ! makes it false, and vice versa.
C
int a = 10;
if (!(a > 5)) {
printf("Condition is False\n");
} else {
printf("Condition is True\n");
}
// Output: Condition is True
Truth Tables
Logical AND (&&):
| Operand 1 | Operand 2 | Result |
|---|---|---|
|
True |
True |
True |
|
True |
False |
False |
|
False |
True |
False |
|
False |
False |
False |
Logical OR (||):
| Operand 1 | Operand 2 | Result |
|---|---|---|
|
True |
True |
True |
|
True |
False |
True |
|
False |
True |
True |
|
False |
False |
False |
Logical NOT (!):
| Operand | Result |
|---|---|
|
True |
False |
|
False |
True |
Example Program
C
#include <stdio.h>
int main() {
int a = 10, b = 5, c = 0;
// Logical AND
if (a > b && b > c) {
printf("Both conditions are true.\n");
}
// Logical OR
if (a > b || c > b) {
printf("At least one condition is true.\n");
}
// Logical NOT
if (!c) {
printf("c is zero (false).\n");
}
return 0;
}
Output:
Both conditions are true.
At least one condition is true.
c is zero (false).
Short-Circuit Evaluation
- AND (&&): If the first operand is false, the second operand is not evaluated because the result is already determined to be false.
- OR (||): If the first operand is true, the second operand is not evaluated because the result is already determined to be true.
Example:
C
int a = 5, b = 0;
if (a > 10 && b++) {
// b++ is not executed because a > 10 is false.
}
printf("b = %d\n", b); // Output: b = 0
Operator Precedence
| Operator | Precedence | Associativity |
|---|---|---|
|
! |
High |
Right-to-left |
|
&& |
Medium |
Left-to-right |
|
` |
` |
Example:
C
int a = 5, b = 10, c = 15;
if (a < b || b > c && c > a) {
// Evaluates as: b > c && c > a (true), then a < b || true (true)
printf("True\n");
}
Applications
- Decision Making:
- Combining multiple conditions in if statements.
C
if (age > 18 && age < 60) {
printf("Eligible\n");
}
- Loops:
- Controlling loop execution with complex conditions.
C
while (x > 0 && y < 100) {
// Perform operations
}
- Validations:
- Checking multiple constraints for input validation.
C
if (score >= 0 && score <= 100) {
printf("Valid score.\n");
}
Logical operators are essential for writing efficient and readable conditional expressions in programming. They provide flexibility in evaluating complex scenarios with minimal code.
