sách gpt4 ăn đã đi

转换为 bool : `!!` vs `(bool)`

In lại 作者:太空狗 更新时间:2023-10-29 16:00:16 27 4
mua khóa gpt4 giày nike

sử dụng !!x 有什么区别吗?对比(bool)x ?

假设__STDC_VERSION__ >= 199901L#include

他们都保证结果是0吗?或 1 ,并且无论 x 的大小和值如何,都不会发生溢出?

câu trả lời hay nhất

!!x (在 C 中,不是 C++)具有类型 số nguyên . (bool)x (包含 )类型为 _Bool .同时 _Bool s很快被提升为số nguyên s,这两个可以从里面分辨出来一个_Generic .

!!x (或等效的 0!=x )将始终是 0hoặc 1(bool)x永远是 (bool)0hoặc (bool)1并且如果它编译(=不违反任何约束)并且x被定义,它总是被明确定义。

!!x+INT_MAX 这样的表达式溢出或 (bool)x+INT_MAX ( bool 提升为 số nguyên )仍然是一个问题。


引用资料:

6.3.1.2 : (_Bool)x <=> (_Bool)(0!=x)

1 When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0; otherwise, the result is 1.59)

( 6.2.5p18 , 6.2.5p21 -- 标量是数字类型或指针

Integer and floating types are collectively called arithmetic types. ...

Arithmetic types and pointer types are collectively called scalar types. ... )

6.5.3.3p5 :!!x <=> 0!=x

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. The result has type int. The expression !E is equivalent to (0==E).

关于转换为 bool : `!!` vs `(bool)` ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54958707/

27 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress