Logical Not Operator In C

The logical AND operator is employed to evaluate the validity of multiple operands in an expression; it yields a true or non-zero (1) result when all conditions are met. Conversely, it produces a false or 0 output otherwise. Therefore, the logical AND operator functions effectively only if all operand conditions are satisfied; otherwise, it outputs 0. This operator is symbolized by '&&' in programming languages.

Syntax:

Example

(A > b && b > c)

The logical OR operator evaluates both conditions of operands A and B. When either of the expressions is true, the operator will yield a true Boolean value. Conversely, if neither expression is true, it will result in a false or zero value. Represented by the '||' double pipe symbol, the logical OR operator is an essential component in programming.

Syntax:

Example

(A > B) || (A < C)

Logical NOT operator

The '!' symbol signifies the logical NOT operator, which is employed to invert the outcome of a provided expression or condition. When an expression yields a non-zero or true value, the outcome is flipped to zero or false. Likewise, if the result of a condition is false or 0, the NOT operator inverts the result to return 1 or true.

For instance, if a user inputs a value of 5 that is not zero, the logical NOT (!) operator will yield the Boolean value of 0, which represents false. Conversely, when the user inputs a value of zero (0), the operator will output the Boolean value of 1, representing true.

Syntax of the logical NOT operator

Example

! (condition); // It '!' represents the NOT operator

Here, the '!' symbol denotes the logical NOT operator, which reverses the outcome of the specified condition.

The truth table of the logical NOT operator:

Below is the truth table illustrating the functionality of the logical NOT operator in the C programming language:

A !A
0 1
1 0
Example

condition	!(condition)
1             0
0             1

Example 1: Program to use the logical NOT operator in C

Let's develop a basic program in C language to invert the order of the operands in a given condition.

Example

/* demonstration the use of the logical not operator in C. */
#include <stdio.h>
#include <conio.h>
int main ()
{
	// declare an initialize x variable
	int x = 5;
	
	// display the result generated using the NOT (!) operator
	printf (" The return value = %d \n", ! (x == 5));
	printf (" The return value = %d \n", ! (x != 5));
	printf (" The return value = %d \n", ! (x >= 3));
	printf (" The return value = %d \n", ! (x < 3));
	
	return 0;
}

Output:

Output

The return value = 0
The return value = 1
The return value = 0
The return value = 1

In the preceding code snippet, the NOT (!) operator is applied to invert the outcome of different expressions. For instance, when evaluating whether the variable x equals 5, yielding true, the NOT operator flips the result to return 0. Likewise, for the condition (x!=5), the logical operator alters the outcome to return 1, and so forth.

Example 2: Program to input a number to perform the logical NOT operator

Let's develop a basic program to obtain the inverse value of an integer by utilizing the logical NOT (!) operator in the C programming language.

Example

/* demonstration the use of the logical not operator in C. */
#include <stdio.h>
#include <conio.h>
int main ()
{
	// declare an initialize x variable
	int x, n;
	printf (" Enter the number: ");
	scanf ("%d", &x);
	
	n = !x; // use logical not operator to reverse the condition
	printf (" The result of x: %d", n); // display the result
	
	return 0;
}

Output:

Output

Enter the number: 7
The result of x: 0

In the above program, we input an integer number 7 from the user and store it into x variable. After that, the logical NOT (!) operator reverses the value of x (non-zero) and returns zero (0) to print the result of x.

2 nd execution:

Example

Enter the number: 0
The result of x: 1

Similarly, we input zero (0) from the user and use the logical NOT (!) operator to reverse the value of x to a non-zero value, which is 1.

Example 3: Program to find the leap year using the logical AND (&&), OR (||), and NOT (!) operator

Let's create a basic program in C to determine if a specific year is a leap year by utilizing the logical AND (&&), logical OR (||), and logical NOT (!) operators.

Example

#include <stdio.h>
#include <conio.h>

int main ()
{
	int yr; // declare int type variable
	
	printf (" Enter the year: ");
	scanf ("%d", &yr);
	
	// use the if..else statement to check the condition
	/* '&&' (AND) operator to validate both operand, '||' (OR) operator check ny given expressions are true,
	 '!' (NOT) check the result of (yr % 100 != 0). */ 
	if ( (yr % 400 == 0) || (yr % 4 == 0 && yr % 100 != 0))
	{
		printf (" %d is a leap year. \n", yr);
	}
	else
	{
		printf (" %d is not a leap year. \n", yr);
	}
	return 0;
}

Output:

Output

Enter the year: 2020
2020 is a leap year.

In the above program, we enter 2020 and then check the given year by defining the if...else statement. In this statement, we defined two conditions;

  • The given year is divided by 400, which is equal to 0. And then, we use the logical OR operator to check whether the left or right operand condition is true.
  • In the second condition, the given year is divided by 4 and 100. But when we divide 2020 with 4, which is equal to 0. Similarly, we divide the year 2020 by 100, which is also not equal to 0. So, both the conditions are true that display the "2020 is a leap year".
  • But when we enter the year 2021, it prints the given result "2021 is not a leap year".

2 nd execution:

Example

Enter the year: 2021
2021 is not a leap year.

Example 4: Code to verify various conditions utilizing the AND, OR, and NOT logical operators.

Let's develop a program to showcase various scenarios involving the provided operands by utilizing the AND, OR, and NOT logical operators in the C programming language.

Example

/* program to check the various condition using the logical NOT operator in c. */
#include <stdio.h>
#include <conio.h>

int main ()
{
	// declare and initialize variables
	int a = 20, b = 15;
	int n1 = 15, n2 = 17;
	
	// use logical 'AND' and logical 'NOT' operator
	if (a > b && a != 0)
	{
		printf (" The AND (&&) operator said: Both conditions are true. \n ");		
	}
	
	// use logical 'OR' and logical 'NOT' operator
	if (n1 > n2 || n2 != 15)
	{
		printf (" The OR (||) operator said: Only one condition is true. \n ");
	}
	
	if ( ! (a > b && a != 0 ))
	{
		printf (" The NOT (!) operator: Here both conditions are true. \n ");		
	}
	else
	{
		printf (" The NOT (!) operator: Here, both conditions are true. " 
		" But, the status of the condition is reversed as false. \n");
	}
	return 0;
}

Output:

Output

The AND (&&) operator is said: Both conditions are true.
The OR (||) operator is said: Only one condition is true.
The NOT (!) operator: Here, both conditions are true. But, the status of the condition is reversed as false.

Input Required

This code uses input(). Please provide values below: