刺激战场
  • 4730阅读
  • 1回复

Question9.1:What is the right type to use for Boolean values in C?Is there a standard type? [复制链接]

上一主题 下一主题
离线那个谁
 

只看楼主 倒序阅读 使用道具 楼主  发表于: 2013-07-15
— 本帖被 那个谁 从 果子玩转Linux 移动到本区(2013-11-17) —

Q: What is the right type to use for Boolean values in C? Is there a standard type? Should I use #defines or enums for the true and false values?
A: Traditionally, C did not provide a standard Boolean type, partly and partly to allow the programmer to make the appropriate space/time tradeoff. (Using an int may be faster, while using char may save data space. [footnote] Smaller types may make the generated code bigger or slower, though, if they require lots of conversions to and from int.)
However, C99 does define a standard Boolean type, as long as you include <stdbool.h>.
If you decide to define them yourself, the choice between #defines and enumeration constants for the true/false values is arbitrary and not terribly interesting (see also questions 2.22 and 17.10). Use any of
              #define TRUE  1    
              #define YES 1
              #define FALSE 0    
              #define NO  0
              enum bool {false, true};    
              enum bool {no, yes};
or use raw 1 and 0, as long as you are consistent within one program or project. (An enumeration may be preferable if your debugger shows the names of enumeration constants when examining variables.)
You may also want to use a typedef:
                          typedef int bool;
     or                  typedef char bool;
     or                 typedef enum
                         {
                                 false, true
                            } bool;
Some people prefer variants like
                 #define TRUE (1==1)
                 #define FALSE (!TRUE)
or                  define ``helper'' macros such as
                #define Istrue(e) ((e) != 0)
These don't buy anything (see question 9.2; see also questions 5.12 and 10.2).
离线那个谁

只看该作者 沙发  发表于: 2013-07-15
问题:在C语言中使用布尔值的正确的类型是什么?有一个标准的类型吗?我是否应该使用#define或者enums来定义真假值?
回答:传统的C语言没有提供一个标准的布尔类型,而是允许程序员自己做一个时间或者空间上的折中。(使用int可能更快一点,使用char可能会节省数据空间。如果需要跟int类型做一个转换,那么更小的类型可能产生更大或更慢的代码)。
可是在C99标准中,当你包含了<stdbool.h>头文件后,那么你就可以定义一个标准的布尔类型了。
如果你决定你自己定义它们,那么#defines和枚举常量来定义真假值两个选择可以任意挑选(参考问题2.22和17.10)。使用任意一种:
        #define TRUE  1    
              #define YES 1
              #define FALSE 0    
              #define NO  0
              enum bool {false, true};    
              enum bool {no, yes};
主要能在同一程序或者项目中保持一致,也可以使用1或者0(如果调试器在查看变量的时候能够显示枚举常量的名字,那么使用枚举可能更后一点)。
同样也可以使用typedef:
              typedef int bool;
     or                  typedef char bool;
     or                 typedef enum
                         {
                                 false, true
                            } bool;
有些人更喜欢一些变异的类型,像
               #define TRUE (1==1)
                        #define FALSE (!TRUE)
或者                 定义`辅助''宏指令,如
                #define Istrue(e) ((e) != 0)
但是这样做没有什么益处(参考问题9.2;参考问题5.12和10.2)。
(注意:布尔型变量只有两种状态,做条件的时候直接出现在条件中,不用和0或1进行比较)
快速回复
限100 字节
如果您提交过一次失败了,可以用”恢复数据”来恢复帖子内容
 
上一个 下一个