# Logical operators tutorial with Examples | Logical operators in C language usage and Examples

In Previous blog post i discussed about Relational operators. We have six relational operators. Those are

<  ,  <=   ,>  ,>  =,  ==,   =!
We know that these relational operators are used to find relation between two Expressions or Numbers or anything. Now we have situation like this  if the value of the variable X is both greater than zero and less than two?  None of these relational operators by itself can answer that question.we need to combine two relational operators. like this
(X  > 0)  and  (X < 2).
Here Logical operators comes into the Picture. here and is Logical operator. C have Three Logical operators:
Let us take two variables X and Y. And Assume X = 1 and Y =0.
 Operator Symbol Example Usage Result && AND X&&Y 0 || OR X||Y 1 ! NOT !X 0
Now our above example can be evaluated by using two relational operators and one Logical operator.like this.
(X > 0) && (X < 2)
Quick Notes :

Logical operators return 0 for False and 1 for True.The Operand for Logical operator may be Constant, Variable or Expression.

### Logical AND(&&) Operator With Example and Truth table :

If the both operands are Non-zero then only Condition TRUE. in all other cases Condition False.
here is the truth table for Logical AND.
 A B A&&B 0 0 0 0 1 0 1 0 0 1 1 1

### Example Program to Understand Logical And operator :

1. #include<stdio.h>
2. int main()
3. {
4.         printf(“Logical AND Operator Result : n);
5.         printf(” 0 && 0 = %d n,0&&0);
6.         printf(” 1 && 0 = %d n,1&&0);
7.         printf(” 0 && 1 = %d n,0&&1);
8.         printf(” 1 && 1 = %d n,1&&1);
9.         return 0;
10. }
Output :

Logical AND Operator Result :  0 && 0 = 0  1 && 0 = 0  0 && 1 = 0  1 && 1 = 1

### Logical OR ( || ) Operator With Example and Truth table :

If the both operands are zero then only Condition False. in all other cases Condition TRUE.
here is the truth table for Logical OR.
 A B A||B 0 0 0 0 1 1 1 0 1 1 1 1

### Example Program to Understand Logical OR ( || ) operator :

1. #include<stdio.h>
2. int main()
3. {
4.         printf(“Logical OR Operator Result : n);
5.         printf(” 0 || 0 = %d n,0||0);
6.         printf(” 1 || 0 = %d n,1||0);
7.         printf(” 0 || 1 = %d n,0||1);
8.         printf(” 1 || 1 = %d n,1||1);
9.         return 0;
10. }
Ouput:

Logical OR Operator Result :
0 || 0 = 0
1 || 0 = 1
0 || 1 = 1
1 || 1 = 1

### Logical NOT ( ! ) Operator With Example and Truth table :

Logical NOT operator gives Negation value or reverses the state of the operand. if operand Value is Non-zero then Result is Zero. if Operand Value is Zero result is non-zero.
Here is truth table of NOT operator.
 A !A 1 0 0 1

### Example Program to Understand Logical NOT ( ! ) operator

1. #include <stdio.h>
2. int main(void)
3. {
4.        printf(” LOgical NOT Operator Result : n);
5.         printf(” !1 is : %d n,!1);
6.         printf(” !0 is : %d n, !0);
7.         return 0;
8. }
Output :

LOgical NOT Operator Result : !1 is : 0 !0 is : 1 Venkatesh Macha

Hi Guys, I am Venkatesh Macha. I am a programmer and an Open Source enthusiast, Presently working as a Software Engineer. Apart from that, I love to explore new technologies and things.