开运算示例_具有C语言示例的逻辑NOT(!)运算符

开运算示例

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

开运算示例

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值