← Back to C Course | Chapter 3: Operators | Lesson 4 of 9

C Logical Operators

Logical operators combine true-or-false questions, like 'you can go out if it is sunny AND homework is done'. && means and, || means or, and ! means not.
Syntax
c
condition1 && condition2  // AND
condition1 || condition2  // OR
!condition                // NOT

Logical AND (&&)

&& (logical AND) evaluates to true only when both the left and right conditions are true -- useful whenever an action should only happen if multiple requirements are satisfied at once, like checking a value is both positive and below a limit.

Example: Logical AND (&&)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare age and set it to 25
	int age = 25;
	// Declare hasID and set it to 1
	int hasID = 1;
	// Check whether age >= 18 && hasID
	if (age >= 18 && hasID) {
		// Print formatted text to the screen
		printf("Allowed");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Logical OR (||)

|| (logical OR) evaluates to true if at least one of its two conditions is true -- useful for accepting any of several valid cases, like checking whether input matches one option or another.

Example: Logical OR (||)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare grade and set it to 'B'
	char grade = 'B';
	// Check whether grade == 'A' || grade == 'B'
	if (grade == 'A' || grade == 'B') {
		// Print formatted text to the screen
		printf("Good grade");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Logical NOT (!)

! (logical NOT) flips a condition's truth value: applied to something true it becomes false, and applied to something false it becomes true -- handy for writing a condition as "not X" instead of restructuring the whole check.

Example: Logical NOT (!)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare isLoggedIn and set it to 0
	int isLoggedIn = 0;
	// Check whether !isLoggedIn
	if (!isLoggedIn) {
		// Print formatted text to the screen
		printf("Please log in");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Short-Circuit Evaluation

C's && and || operators short-circuit: in a && b, if a is already false, b is never evaluated at all, since the overall result is already determined -- this matters when b has side effects or could cause an error if evaluated unnecessarily.

Example: Short-Circuit Evaluation

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare a and set it to 0
	int a = 0;
	// Check whether a != 0 && (10 / a > 1)
	if (a != 0 && (10 / a > 1)) {
		// Print formatted text to the screen
		printf("unreachable");
	}
	// Print formatted text to the screen
	printf("Safe: division skipped");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Combining Logical Operators

Combining several logical operators in one expression without parentheses relies on C's default precedence rules, which can be easy to misjudge -- wrapping each sub-condition in parentheses makes the intended grouping unambiguous at a glance.

Example: Combining Logical Operators

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare age and set it to 20
	int age = 20;
	// Declare hasTicket and set it to 1
	int hasTicket = 1;
	// Run this block only if the condition is true
	if ((age >= 18) && (hasTicket || age >= 65)) {
		// Print formatted text to the screen
		printf("Entry allowed");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using & instead of &&, which is a bitwise operator and can behave differently.
  2. Writing if (x == 1 || 2), which is always true because 2 is non-zero.
  3. Relying on the right side of && or || always running when short-circuiting may skip it.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.