Logical NOT (!) Operator in C
This section will discuss the logical NOT (!) operator in the C programming language. As we already know, the logical operator is used to perform the logical operation by combining two or more conditions on the given expressions. If the logical conditions of the operands are true, the operator returns true Boolean values or 1. Otherwise, it returns a false Boolean value or 0. The logical operators are classified into three parts: Logical AND, Logical OR, and Logical NOT operators.
The logical AND operator is used to check the conditions of two or more operands that remain are true in a given expression; the AND operator returns a true or non-zero (1) value. Else, it returns a false or 0 value. So, we can say the logical AND operator can perform only in an expression when conditions of both operands are true, and if any condition is not true, it returns 0. The logical AND operator is represented as the double ampersand '&&' symbol.
The logical OR operator is used to check both operands (A & B) conditions, and if one of the operands or expressions is true, the operator returns a true Boolean value. Similarly, if none of the expressions are true, it returns a false or zero value. The logical OR operator is denoted as the double pipe '||' symbol.
Logical NOT operator
The logical NOT operator is represented as the '!' symbol, which is used to reverse the result of any given expression or condition. If the result of an expression is non-zero or true, the result will be reversed as zero or false value. Similarly, if the condition's result is false or 0, the NOT operator reverses the result and returns 1 or true.
For example, suppose the user enters a non-zero value is 5, the logical NOT (!) operator returns the 0 or false Boolean value. And if the user enters a zero (0) value, the operator returns the true Boolean value or 1.
Syntax of the logical NOT operator
Here, the '!' symbol represents the logical NOT operator, which inverses the result of the given condition.
The truth table of the logical NOT operator:
Following is the truth table of the logical not operator in C
Example 1: Program to use the logical NOT operator in C
Let's create a simple program to reverse the given condition of the operands in the C programming language.
The return value = 0 The return value = 1 The return value = 0 The return value = 1
In the above program, we use the NOT (!) operator to reverse the result of the various expression, such as the condition of the variable x is equal to 5, which is true. Still, the NOT operator reverses the result and returns 0. Similarly, we defined the condition (x!=5), but the logical operator changed its result and returned 1 and so on.
Example 2: Program to input a number to perform the logical NOT operator
Let's create a simple program to get the reverse result of an integer number using the logical NOT (!) operator in the C programming language.
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.
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 write a simple program to check whether the given year is a leap or not using the logical AND (&&), logical OR (||), and the logical NOT (!) operator in the C language.
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;
Enter the year: 2021 2021 is not a leap year.
Example 4: Program to check different conditions using the AND, OR, and the NOT logical operator
Let's write a program to demonstrate the multiple conditions of the given operands using the AND, OR, and the NOT logical operator in C.
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.