C中的!0是什么? [英] What is !0 in C?

查看:23
本文介绍了C中的!0是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在 C 语言中,对于 if 语句和比较 FALSE = 0 并且其他任何值都等于 true.

因此,

int j = 40诠释 k = !jk == 0//这是真的

我的问题正好相反.!0 变成了什么?1?

int l = 0int m = !l米==?//什么是 m?

解决方案

C 中的布尔/逻辑运算符需要产生 0 或 1.

来自 ISO C99 标准的第 6.5.3.3/5 节:

<块引用>

如果其操作数的值比较不等于0,则逻辑否定运算符!的结果为0,如果其操作数的值比较等于0,则为1.

事实上,!!x 是一个常见的习惯用法,用于强制将值设为 0 或 1(不过我个人更喜欢 x != 0).

另请参阅 comp.lang.c 常见问题解答中的 Q9.2.p>

I know that in C, for if statements and comparisons FALSE = 0 and anything else equals true.

Hence,

int j = 40
int k = !j

k == 0 // this is true

My question handles the opposite. What does !0 become? 1?

int l = 0
int m = !l

m == ? // what is m?

解决方案

Boolean/logical operators in C are required to yield either 0 or 1.

From section 6.5.3.3/5 of the ISO C99 standard:

The result of the logical negation operator ! is 0 if the value of its operand compares unequal to 0, 1 if the value of its operand compares equal to 0.

In fact, !!x is a common idiom for forcing a value to be either 0 or 1 (I personally prefer x != 0, though).

Also see Q9.2 from the comp.lang.c FAQ.

这篇关于C中的!0是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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