Logical Operators in C

Preview

Logical Operators in C

These operators are used to combine relational expressions. The Output: of a logical expression is always 1 or 0.

Various logical operators in C language are as follows:

Operator

Name of Operator

Purpose

&&Logical ANDTo combine two relational expressions. If any of the input is 0, Output: would be 0. If all the inputs are 1, Output: would be 1.
||Logical ORTo combine two relational expressions. If any of the input is 1, Output: would be 1. If all the inputs are 0, Output: would be 0.
!Logical NOTIt is used to reverse the Output: of a relational or logical expression. If input is 1, Output: would be 0 and vice versa.



1. Logical And(&&)

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

If any input of this operator is 0, Output would be 0.

If all the inputs of this operator are 1, Output would be 1

Truth table for Logical AND operator

Input1

Input2

Output

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.

If any input of this operator is 1, Output would be 1.

If all the inputs of this operator are 0, Output would be 0

Truth table for Logical OR operator

Input1

Input2

Output

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.

If input of this operator is 1, Output would be 0.

If input of this operator is 0, Output would be 1.

Truth table for Logical NOT operator

Input

Output

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;

 

 

printf(“\nEnter value of a=”);
scanf(“%d”,&a);
Enter value of a=10
printf(“\nEnter value of b=”);
scanf(“%d”,&b);
Enter value of 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