C 条件总是返回 1 还是 0? [英] Will a C conditional always return 1 or 0?

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

问题描述

C 条件语句总是返回 [1 或 0],还是返回 [0 或非零"].我问是因为:

Do C conditional statements always return [1 or 0], or do they return [0 or 'something other than zero']. I ask because:

伪代码——

foo(地址,应该发送):
注册 >>= 1
register <<= 1//清除第一个位置的位
register |= shouldSend//表示是否应该发送

foo(address, shouldSend):
register >>= 1
register <<= 1 // to clear bit in first position
register |= shouldSend // to signal whether it should send or not

如果有人传入大于 1 的 shouldSend 值 true(因为只有 0 为 false,其他所有为 true,从技术上讲这是有效的),就会出现问题.因为我直接将 shouldSend 的真值与寄存器进行或运算,所以最好不要说 0xFF!我已经有了一个解决方案,所以这个问题更多是出于好奇.我想知道是否:

the problem occurs if somebody passin IN a shouldSend value of true greater than one (as only 0 is false and all else is true, technically this is valid). since i am directly OR'ing the truth value of shouldSend with the register, it better not be say 0xFF! i already have a solution, so the question is more for curiousity sake. i am wondering though if:

foo(地址,应该发送):
注册 >>= 1
register <<= 1//清除第一个位置的位
register |= (shouldSend > 0)//表示是否应该发送

foo(address, shouldSend):
register >>= 1
register <<= 1 // to clear bit in first position
register |= (shouldSend > 0) // to signal whether it should send or not

解决问题了吗?我认为现在传入的 0xFF(或通常大于 1 的东西)的问题被 C 条件所掩盖.但这仅适用于 IF C 条件保证返回 [0 或 1].

solves the problem? i would think that now the problem of an 0xFF(or in general, something greater than 1) passed in is masked by the C conditional. but this only holds IF C conditionals are guaranteed to return [0 or 1].

ps - 我也意识到它可能依赖于编译器,但是 ansi 标准对此有何看法?

ps - i also realize that it is probably compiler dependent, but what does the ansi standard say about this?

推荐答案

标准规定结果总是整数值等于0或1

Standard specifies that the result is always integer value equals to 0 or 1

6.5.8关系运算符

每个运算符 <(小于), >(大于), <= (小于或等于to) 和 >= (大于或等于)如果指定,则应产生 1关系为真,如果是,则为 0false.92) 结果的类型为 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.92) The result has type int.

这篇关于C 条件总是返回 1 还是 0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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