Javatpoint Logo
Javatpoint Logo

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.

Logical NOT (!) Operator in C

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.

Syntax:

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.

Syntax:

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.

Output:

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.

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.

2nd execution:

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.

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;

  1. 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.
  2. 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".
  3. But when we enter the year 2021, it prints the given result "2021 is not a leap year".

2nd execution:

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.

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.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA