开运算示例
Logical operators work with the test conditions and return the result based on the condition's results, these can also be used to validate multiple conditions together.
逻辑运算符使用测试条件并根据条件的结果返回结果,这些逻辑运算符还可用于一起验证多个条件。
In C programming language, there are three logical operators Logical AND (&&), Logical OR (||) and Logician NOT (!).
在C编程语言中,有三个逻辑运算符Logical AND(&&) , Logical OR(||)和Logician NOT(!) 。
C语言中的逻辑NOT(!)运算符 (Logical NOT (!) operator in C)
Logical NOT is denoted by exclamatory characters (!), it is used to check the opposite result of any given test condition.
逻辑NOT由感叹号( ! )表示,用于检查任何给定测试条件的相反结果。
If any condition's result is non-zero (true), it returns 0 (false) and if any condition's result is 0(false) it returns 1 (true).
如果任何条件的结果为非零 (true),则返回0(false),如果任何条件的结果为0(false),则返回1(true)。
Syntax of Logical NOT operator:
逻辑NOT运算符的语法:
!(condition)
Truth table of logical NOT operator:
逻辑NOT运算符的真值表:
condition !(condition)
1 0
0 1
逻辑NOT(!)运算符的C示例 (C examples of Logical NOT (!) operator )
Example 1: Take a number and apply some of the conditions and print the result of expression containing logical NOT operator.
示例1:取一个数字并应用一些条件,并打印包含逻辑NOT运算符的表达式的结果。
// C program to demonstrate example of
// Logical NOT (!) operator
#include <stdio.h>
int main()
{
int num =10;
//printing result with OR (||) operator
printf("%d\n", !(num==10));
printf("%d\n", !(num!=10));
printf("%d\n", !(num>5));
printf("%d\n", !(num<5));
return 0;
}
Output
输出量
0
1
0
1
Example 2: Input a year and check it is leap year or not (it will use Logical AND (&&), Logical OR (||) and Logical NOT (!) operators).
示例2:输入年份并检查是否为leap年(它将使用逻辑与(&&),逻辑或(||)和逻辑非(!)运算符)。
// C program to demonstrate example of
// Logical NOT (!) operator
// Input a year and check it is leap year or not
#include <stdio.h>
int main()
{
int y;
//input year
printf("Enter year: ");
scanf("%d", &y);
//check condition
if((y%400==0) || (y%4==0 && y%100!=0))
printf("%d is a leap year\n",y);
else
printf("%d is not a leap year\n",y);
return 0;
}
Output
输出量
First run:
Enter year: 2004
2004 is a leap year
Second run:
Enter year: 2100
2100 is not a leap year
Third run:
Enter year: 2400
2400 is a leap year
Fourth run:
Enter year: 2003
2003 is not a leap year
翻译自: https://www.includehelp.com/c/logical-not-operator-with-example-in-c.aspx
开运算示例