A logical operator is a symbol or word that connects two or more expressions so that the value of the created expression is solely determined by the value of the original expressions and the operator’s meaning.
Below are the logical operators available in C:
And
OR
Not
The table below explains each of the logical operators in C:
Operator Symbol | Operator Name | Example | Description |
&& | Logical And | Operand-A && Operand-B | Returns True when both operand-A and operand-B are True; otherwise, it returns False. |
|| | Logical Or | Operand-A || Operand-B | Returns True when either operand-A or operand-B is True; otherwise, it returns False. |
! | Logical Not | ! Operand-A | Returns the operand's logical state in reverse. |
The figure below is the visual representation of the logical operators in C:
Note:
True
equates to1
.False
equates to0
.
The following code demonstrates how to use these logical operators in C.
#include <stdio.h>//Inorder to used bool datatype#include <stdbool.h>int main(){int A=10, B=20, C=30;//And Operator//Both Trueprintf("The value of (A<B && B<C): %d \n", (A<B && B<C));//First operand: True Second Operand: Falseprintf("The value of (A<B && B>C): %d \n", (A<B && B>C));//Both Falseprintf("The value of (A>B && B>C): %d \n", (A>B && B>C));//OR operator//Both Trueprintf("The value of (A<B || B<C): %d \n", (A<B || B<C));//First operand: True Second Operand: Falseprintf("The value of (A<B || B>C): %d \n", (A<B || B>C));//Both Falseprintf("The value of (A>B || B>C): %d \n", (A>B || B>C));//Not operatorbool D=true;//reverse of true logical stateprintf("The value of (!D): %d \n", (!D));return 0;}