C NOT Logical Operator
C NOT Logical Operator is used to inverse the result of a boolean value or an expression. !
is the symbol used for NOT Logical Operator in C programming.
C NOT Operator takes only one boolean value as operand and returns the result of NOT operation.
</>
Copy
!operand
Truth Table
The following is truth table for NOT Logical Operator.
Operand | Returns |
true | 0 |
false | 1 |
Examples
In the following program, we demonstrate the result of NOT logical operator (!) with a boolean value.
main.c
</>
Copy
#include <stdio.h>
#include <stdbool.h>
int main() {
bool x, result;
x = true;
//NOT Operation
result = !x;
printf("!true : %d\n", result);
}
Output
!true : 0
Program ended with exit code: 0
If you are using bool keyword in the program, include stdbool.h library.
In the following program, we demonstrate how to use NOT logical operator in an If condition.
main.c
</>
Copy
#include <stdio.h>
int main() {
int x = 7;
if (!(x % 2 == 0)) {
printf("%d is odd number.\n", x);
}
}
Output
7 is odd number.
Program ended with exit code: 0
Conclusion
In this C Tutorial, we learned what C NOT Logical Operator is, and how to use it with conditional expressions in C programming.