Logical NOT operator- programming tutorial

01 Jul 2023 Balmiki Mandal 0 C Programming

The Logical NOT (!) Operator in C

The logical NOT (!) operator in C is used to reverse the logical state of its operand. If a condition is true, then the logical NOT operator will make it false. And if it is false, it will return true.

syntax for the logical NOT operator is:

!operand

where operand is an expression that evaluates to a Boolean value.

For example, the following code:

int a = 10;
int b = 20;

int result1 = !(a == 10);
int result2 = !(b == 20);

will return 0 and 1, respectively. This is because the first condition, a == 10, is true, so the logical NOT operator reverses it to false. The second condition, b == 20, is false, so the logical NOT operator reverses it to true.

The logical NOT operator is often used in decision-making statements, such as if statements and while loops. For example, the following code:

if (!(a == 10)) {
  printf("The value of a is not equal to 10.\n");
}

will print the message "The value of a is not equal to 10" if the value of a is not equal to 10.

Here is a table of the truth values for the logical NOT operator:

operand | result
------- | --------
0       | 1
1       | 0

As you can see, the logical NOT operator returns 1 if the operand is false, and 0 if the operand is true.

 

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.