Logical Operators in C

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. If any input is 0, output is 0. If all inputs are 1, output is 1.

Truth table for Logical AND operator

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

Example

110 >12 && 110>15 = 1
110 >12 && 110<15 = 0

2. Logical OR(||)

Logical OR operator is represented as double pipe sign (||). It is used to combine two relational expressions. If any input is 1, output is 1. If all inputs are 0, output is 0.

Truth table for Logical OR operator

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

 Example

110 >12 || 110>15 = 1
110 <12 || 110<15 = 0

3. Logical NOT(!)

Logical NOT operator is represented as sign of exclamation(!).  If input is 0, output is 1 and vice versa.

Truth table for Logical NOT operator

Input Output
0 1
1 0

 Example

!(110 >12 && 110>15) = 0
!(110<15) = 1

Program to demonstrate
logical operators.
 Output (Line Wise)
#include<stdio.h>
#include<conio.h>
int  main()
{
int a,b,c;

 

 

a=110
b=12
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