Tag: 布尔

_Bool数据类型为C99

C编程语言的C99标准将_Bool数据类型定义为另一种数据类型的宏(因为该语言无法处理类型安全布尔值)。 _Bool是unsigned char , unsigned int或其他一些数据类型的宏吗?

布尔值的排序

在C ++的C ++或下,如何为布尔值定义小于运算符? 或者,解释此代码的行为: #ifndef __cplusplus #include #endif #include int main() { bool b = -1; if(b < true) { printf("b < true\n"); } if(b < false) { printf("b < false\n"); } if(true < false) { printf("true < false\n"); } if(false < true) { printf("false < true\n"); } } 在MSVC版本10下,编译为C ++代码,GCC 4.6.3-ubuntu5编译为C代码,G ++ 4.6.3-1ubuntu5编译为C ++代码,所有你得到的是 […]

与stdbool.h C ++接口

在一个项目中,我在C ++和一个使用stdbool.h的C库之间进行接口。 #ifndef _STDBOOL_H #define _STDBOOL_H /* C99 Boolean types for compilers without C99 support */ /* http://www.opengroup.org/onlinepubs/009695399/basedefs/stdbool.h.html */ #if !defined(__cplusplus) #if !defined(__GNUC__) /* _Bool builtin type is included in GCC */ typedef enum { _Bool_must_promote_to_int = -1, false = 0, true = 1 } _Bool; #endif #define bool _Bool #define true 1 #define false […]

如何在不使用if或for的情况下查找数字是正数,负数还是零?

我想在微处理器中实现符号和零标志设置。 因此,我需要编写一个函数来查找数字是正数,负数还是零而不使用if或for循环,并且只允许使用布尔和位运算符 。 我做了以下。 但是如何实现zero条件呢? int status (int x) { int sign = (x >> 31); return sign; } 有什么建议 ?

链接Bool值给出了预期的相反结果

我不假思索地编写了一些代码来检查结构的所有值是否都设置为0.为了实现这一点,我使用了: bool IsValid() { return !(0 == year == month == day == hour == minute == second); } 其中所有struct成员都是unsigned short类型。 我使用代码作为更大测试的一部分,但注意到它对于不等于零的值返回false,对于全部等于零的值则返回true – 与我预期的相反。 我将代码更改为: bool IsValid() { return (0 != year) || (0 != month) || (0 != day) || (0 != hour) || (0 != minute) || (0 != second); } 但是想知道是什么导致了奇怪的行为。 这是优先的结果吗? […]

C:使用&&运算符时是否存在“惰性求值”,就像在C ++中一样?

我想知道这看起来是否正确: while((next !=NULL) && (strcmp(next->name, some_string) < 0) { //some process } 我的意思是,如果next是NULL ,那么表达式的第二部分将不会被编译器测试过? 我听说在C ++中就是这种情况(但我甚至都不确定)。 有人可以证实我在某些编译器上不会出现奇怪的错误吗?

为什么像(0 <a <5)这样的条件总是正确的?

我在C中实现了以下程序 #include int main() { int a = 10 ; if(0 < a < 5) { printf("The condition is true!") ; } return 0 ; } 为什么条件0<a<5总是返回true ?