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

查看:101
本文介绍了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(address,shouldSend) :

寄存器>> = 1

寄存器<< = 1 //清除第一位置的位

寄存器| = 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值(因为只有0为假),就会出现问题而其他所有条件都是正确的,从技术上讲这是有效的)。由于我直接在将ORSENDSEND的真实值与寄存器进行运算,因此最好不要说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(address,shouldSend):

注册>> = 1

寄存器<< = 1 //清除第一个位置中的位

寄存器| = (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
关系为true,如果
为false,则为0。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天全站免登陆