Logical Operators in C Program
The following table shows, how to use logical operators.
Operator | Description | Example |
---|---|---|
&& | AND Operator. If both the operands are true, then condition becomes true. |
If A is true B is true then
(A && B) is true .
|
|| | OR Operator. If any one of the operands is true, then condition becomes true. |
If A is true B is false then
(A || B) is true .
|
! | NOT Operator. It is used to reverse the state.(true=>false,false=>true ) |
If A is true then
!A is false .
|
Example : pgm.c
#include<stdio.h> int main(){ int a=10; int b=10; printf("\na==10&&b==10 : %d",a==10&&b==10); printf("\na==10&&b==20 : %d",a==10&&b==20); printf("\na==10||b==10 : %d",a==10||b==10); printf("\na==10||b==30 : %d",a==10||b==30); printf("\na==20||b==30 : %d",a==20||b==30); printf("\na!=11 : %d",a!=11); return 0; }
Output:
a==10 && b==10 : 1
a==10 && b==20 : 0
a==10 || b==10 : 1
a==10 || b==30 : 1
a==20 || b==30 : 0
a != 11 : 1
a==10 && b==20 : 0
a==10 || b==10 : 1
a==10 || b==30 : 1
a==20 || b==30 : 0
a != 11 : 1