Skip to content

C 语言重拾【三】新的 _Bool 类型

轩辕十四
Published date:

在 C 语言中,一直用 int 类型的变量表示 真/假 值。C99 专门针对这种类型的变量新增了 _Bool 类型。该类型是以英国数学家 George Boole 的名字命名的,他开发了用代数表示逻辑和解决逻辑问题。在编程中,表示真或假的变量被称为布尔变量(Boolean variable),所以日 _Bool 是 C 语言中布尔变量的类型名。_Bool 类型的变量只能储存 1(真)0(假)。如果把其他非零数值赋给 _Bool 类型的变量,该变量会被设置为 1。这反映了 C 把所有的非零值都视为真

// boolean.c 一一 使用 _Bool 类型的变量 variable
#include <stdio.h>
int main (void) {
  long num;
  long sum = 0L;
  _Bool input_is_good;
  
  printf ("Please enter an integer to be summed ");
  printf (" (q to quit): ");
  input_is_good = (scanf("%ld", &num) == 1);
  while (input_is_good) {
    sum = sum + num;
    printf ("Please enter next integer (q to quit): ");
    input_is_good = (scanf ("%ld", &num) == 1);
  }
  printf ("Those integers sum to %ld. \n", sum);
  
  return 0;
}

注意程序中把比较的结果赋值给 _Bool 类型的变量 input_is_good:

input_is_good = (scanf ("%ld", &num) == 1);

这样做没问题,因为 == 运算符返回的值不是 1 就是 0。顺带一提,从优先级方面考虑的话,并不需要用圆括号把 scanf("%ld", &num) == 1 括起来。但是,这样做可以提高代码可读性。还要注意,如何为变量命名才能让 while 循环的测试简单易懂:

while (input_is_good)

C99 提供了 stdbool.h 头文件,该头文件让 bool 成为 _Bool 的别名,而且还把 truefalse 分别定义为 1 和 0 的符号常量。包含该头文件后,写出的代码可以与 C++ 兼容,因为 C++ 把 booltruefalse 定义为关键字。

如果系统不支持 _Bool 类型,导致无法运行该程序,可以把 _Bool 替换成 int 即可。

Table of contents

Open Table of contents

参考文献

Previous
C 语言重拾【四】ctype.h 系列的字符函数
Next
C 语言重拾【二】字符串