• Home
  • 3.12 Logical NOT

3.12 Logical NOT

View Categories

3.12 Logical NOT

< 1 min read

Table of Contents

The logical NOT operator ! reverses the logical value of its operand. It produces 1 when the operand is zero and 0 when the operand is non-zero.

#include <stdio.h>

int main()
{
    int a = 0;
    int b = 10;

    printf("!a = %d\n", !a);
    printf("!b = %d\n", !b);

    return 0;
}

Example Output #

!a = 1
!b = 0

Explanation #

For a:

!a

Since a is 0, it is logically false. The ! operator reverses it, producing 1.

For b:

!b

Since b is non-zero, it is logically true. The ! operator reverses it, producing 0.

The logical NOT operator follows this behavior:

Operand ! Operand
0 1
non-zero 0

The result of the logical NOT operator is always 0 or 1.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *