是否“真"?在C中总是表示1? [英] Does "true" in C always mean 1?

查看:69
本文介绍了是否“真"?在C中总是表示1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下C代码:

if ((value & 1) == 1)
{

}

假设值等于1,(value & 1)会返回1还是任何未指定的非零数字?

Assuming value equals 1, will (value & 1) return 1 or any unspecified non zero number?

推荐答案

§6.5.8关系运算符

每个运算符<如果指定的关系为true,则(小于),>(大于),< =(小于或等于)和> =(大于或等于)应产生1,如果为false,则产生0.)结果的类型为int.

Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false.) The result has type int.

§6.5.9相等运算符

==(等于)和!=(不等于)运算符与关系运算符类似,不同之处在于它们的优先级较低.)如果指定的关系为true,则每个运算符的结果为1,如果为false,则结果为0. .结果的类型为int.对于任何一对操作数,其中一个关系都是正确的.

The == (equal to) and != (not equal to) operators are analogous to the relational operators except for their lower precedence.) Each of the operators yields 1 if the specified relation is true and 0 if it is false. The result has type int. For any pair of operands, exactly one of the relations is true.

§6.5.13逻辑AND运算符

&&如果运算符的两个操作数都不等于0,则运算符应产生1;否则,运算符应产生1.否则,结果为0.结果的类型为int.

The && operator shall yield 1 if both of its operands compare unequal to 0; otherwise, it yields 0. The result has type int.

§6.5.14逻辑或运算符

||如果运算符的两个操作数都不等于0,则运算符应产生1;否则,运算符应产生1.否则,结果为0.结果的类型为int.

The || operator shall yield 1 if either of its operands compare unequal to 0; otherwise, it yields 0. The result has type int.

这篇关于是否“真"?在C中总是表示1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆