我很高兴,在C中,这样的事情是不好的代码:
(var_a == var_b) ? TRUE : FALSE
Run Code Online (Sandbox Code Playgroud)
但是,处理这个问题的最佳方法是什么:
/* Header stuff */
#define INTERESTING_FLAG 0x80000000
typedef short int BOOL;
void func(BOOL);
/* Code */
int main(int argc, char *argv[])
{
unsigned long int flags = 0x00000000;
... /* Various bits of flag processing */
func(flags & INTERESTING_FLAG); /* func never receives a non-zero value
* as the top bits are cut off when the
* argument is cast down to a short
* int
*/
}
Run Code Online (Sandbox Code Playgroud)
它是否可以接受(对于你所使用的任何可接受的值)(flags & FLAG_CONST) ? TRUE : FALSE?
我会在任何一种情况下调用func (flags & INTERESTING_FLAG) != 0作为参数来指示需要布尔参数而不是算术结果flags & INTERESTING_FLAG.