Logical Operator in C

Logical Operator in C

Logical operators in C can be made clear by the two connectors used in the English language, i.e. and and or.
‘A and B’ means both A and B.

‘A or B’ means either A or B

Similarly, in C programming,

‘A and B’ returns true if both ‘A’ and ‘B’ are true.

‘A or B’ returns true if either ‘A’ or ‘B’ or both are true.

The symbol used for AND is && and for OR is | |.

&& -> Logical AND. If both the operands return true, then the condition becomes true.

| |    -> Logical OR. If either or both the operands return true, then the condition becomes true.

!    -> Logical NOT. It reverses the condition, i.e. if it is true, it returns false, and vice versa using ‘!’.

Example

#include<stdio.h>

int main()

{

            int x,y=17;

            x=22/11+5*(9/3);

           

            printf("%d",   (x==y)&&(x>=y));

            printf("\n%d", (x<y)||(x>y)  );

            printf("\n%d", (!(x>=y))     );

           

}

 

Output

Logical Operator in C

Evaluating the value of x will give ‘17’ as a result.

Thus, ‘x’ and ‘y’ become equal. Therefore, the first printf() statement returns (true) 1, and the rest are false. Hence, the output is 0.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.