Preview

# Logical Operators in C

There are three logical operators.

## 1. Logical And(&&)

Logical And operator is represented as double ampersand sign (&&). It is used to combine two relational expressions.

Truth table for Logical AND operator

 Input1 Input2 Output 0 0 0 0 1 0 1 0 0 1 1 1

Example

10 >3 && 10>5 = 1
10 >3 && 10<5 = 0
10 <3 && 10<5 = 0

## 2. Logical OR(||)

Logical OR operator is represented as double pipe sign (||). It is used to combine two relational expressions.

Truth table for Logical OR operator

 Input1 Input2 Output 0 0 0 0 1 1 1 0 1 1 1 1

Example

10 >3 || 10>5 = 1
10 >3 || 10<5 = 1
10 <3 || 10<5 = 0

## 3. Logical NOT(!)

Logical NOT operator is represented as sign of exclamation(!). It is used to reverse the output of a relational expression or logical expression.

Truth table for Logical NOT operator

 Input Output 0 1 1 0

Example

!(10 >3 && 10>5) = 0
!(10<5) = 1
!(10 <3 || 10<5) = 1

 Program to demonstrate logical operators. Output (Line Wise) #include #include int  main() { int a,b,c; a=10 b=3 printf(”\n%d”, (a>0 && b>0); 1 printf(”\n%d”, (a<0 && b>0); 0 printf(”\n%d”, (a>0 || b<0); 1 printf(”\n%d”, (a<0 && b<0); 0 printf(”\n%d”, !(a>0); 0 printf(”\n%d”, !(a<0 && b>0); 1 getch(); return (0); }

Lesson tags: C Operators, logical and in c, logical not in c, logical opearots of c, logical or in c
Back to: C Programming Language