What are the logical operators and, or, & not in C?

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

Description

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:

Visual representation of Logical Operators in C

Note:

  • True equates to 1.
  • False equates to 0.

Code

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 True
printf("The value of (A<B && B<C): %d \n", (A<B && B<C));
//First operand: True Second Operand: False
printf("The value of (A<B && B>C): %d \n", (A<B && B>C));
//Both False
printf("The value of (A>B && B>C): %d \n", (A>B && B>C));
//OR operator
//Both True
printf("The value of (A<B || B<C): %d \n", (A<B || B<C));
//First operand: True Second Operand: False
printf("The value of (A<B || B>C): %d \n", (A<B || B>C));
//Both False
printf("The value of (A>B || B>C): %d \n", (A>B || B>C));
//Not operator
bool D=true;
//reverse of true logical state
printf("The value of (!D): %d \n", (!D));
return 0;
}

Free Resources