找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 339|回复: 1

Question9.1:What is the right type to use for Boolean values in C?Is there a standard type?

[复制链接]

210

主题

371

回帖

0

积分

管理员

积分
0
发表于 2013-7-15 14:56:30 | 显示全部楼层 |阅读模式

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&#39;&#39; macros such as
                #define Istrue(e) ((e) != 0)
These don&#39;t buy anything (see question 9.2; see also questions 5.12 and 10.2).

210

主题

371

回帖

0

积分

管理员

积分
0
 楼主| 发表于 2013-7-15 15:19:41 | 显示全部楼层
问题:在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)
或者                 定义`辅助&#39;&#39;宏指令,如
                #define Istrue(e) ((e) != 0)
但是这样做没有什么益处(参考问题9.2;参考问题5.12和10.2)。
(注意:布尔型变量只有两种状态,做条件的时候直接出现在条件中,不用和0或1进行比较)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

果子博客
扫码关注微信公众号

Archiver|手机版|小黑屋|风叶林

GMT+8, 2026-2-2 02:53 , Processed in 0.072160 second(s), 20 queries .

Powered by 风叶林

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表