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.

Truth table for Logical AND operator

Input1Input2Output
000
010
100
111

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

Input1Input2Output
000
011
101
111

 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

InputOutput
01
10

 Example

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

 

Program to demonstrate
logical operators.
 Output (Line Wise)
#include<stdio.h>
#include<conio.h>
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