Cprogramming 简明教程

Booleans in C

intcharfloat 类型不同,ANSI C 标准没有内置的或主要的布尔类型。布尔或 bool 数据通常是指可以保存两个二进制值之一的数据:真或假(或“是/否”、“开/关”等)。即使 C 中没有 bool 类型,也可以通过 enum 类型实现布尔的行为。

Unlike the int, char or float types, the ANSI C standard doesn’t have a built-in or primary Boolean type. A Boolean or bool data generally refers to the one that can hold one of the two binary values: true or false (or yes/no, on/off, etc.). Even if the bool type is not available in C, you can implement the behaviour of Booleans with the help of an enum type.

符合 C99 标准或更高版本的新版 C 编译器支持 bool 类型,该类型在头文件 stdbool.h 中定义。

The new versions of C compilers, complying with the C99 standard or later, support the bool type, which has been defined in the header file stdbool.h.

Using enum to Implement Boolean Type in C

枚举类型为整数常量分配用户定义的标识符。我们可以定义一个枚举类型,其中 true 和 false 作为标识符,值分别为 1 和 0。

The enum type assigns user-defined identifiers to integral constants. We can define an enumerated type with true and false as the identifiers with the values 1 and 0.

Example

1 或任何不为 0 的其他数字表示 true,而 0 表示 false。

1 or any other number that is not 0 represents true, whereas 0 represents false.

#include <stdio.h>

int main (){

   enum bool {false, true};
   enum bool x = true;
   enum bool y = false;

   printf ("%d\n", x);
   printf ("%d\n", y);
}

运行代码并检查其输出:

Run the code and check its output −

1
0

typedef enum as BOOL

为了更加简洁,我们可以使用 typedef 关键字按名称 BOOL 调用枚举 bool。

To make it more concise, we can use the typedef keyword to call enum bool by the name BOOL.

Example 1

请看以下示例:

Take a look at the following example −

#include <stdio.h>

int main(){

   typedef enum {false, true} BOOL;

   BOOL x = true;
   BOOL y  = false;

   printf ("%d\n", x);
   printf ("%d\n", y);
}

在这里,你也会获得相同的输出 −

Here too, you will get the same output −

1
0

Example 2

我们甚至可以在决策或循环语句中使用枚举常量 −

We can even use the enumerated constants in the decision-making or loop statements −

#include <stdio.h>

int main(){

   typedef enum {false, true} BOOL;

   int i = 0;

   while(true){
      i++;
      printf("%d\n", i);

      if(i >= 5)
         break;
   }
   return 0;
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

1
2
3
4
5

Boolean Values with

#define 预处理器指令用于定义常量。我们可以使用它来定义布尔常量,FALSE 为 0,TRUE 为 1。

The #define preprocessor directive is used to define constants. We can use this to define the Boolean constants, FALSE as 0 and TRUE as 1.

Example

请看以下示例:

Take a look at the following example −

#include <stdio.h>

#define FALSE 0
#define TRUE 1

int main(){

   printf("False: %d \n True: %d", FALSE, TRUE);

   return 0;
}

运行代码并检查其输出:

Run the code and check its output −

False: 0
 True: 1

Boolean Type in stdbool.h

C 的 C99 标准引入了 stdbool.h 头文件。其中包含 bool 类型的定义,它实际上是 _bool 类型的 typedef 别名。它还定义了宏 true ,它扩展为 1,以及 false ,它扩展为 0。

The C99 standard of C has introduced the stdbool.h header file. It contains the definition of bool type, which actually is a typedef alias for _bool type. It also defines the macros true which expands to 1, and false which expands to 0.

Example 1

我们可以如下使用 bool 类型 −

We can use the bool type as follows −

#include <stdio.h>
#include <stdbool.h>

int main(){

   bool a = true;
   bool b = false;

   printf("True: %d\n", a);
   printf("False: %d", b);

   return 0;
}

执行此代码后,您将获得以下输出 −

On executing this code, you will get the following output −

True: 1
False: 0

Example 2

我们也可以在逻辑表达式中使用 bool 类型变量,如下例所示 −

We can use bool type variables in logical expressions too, as shown in the following example −

#include <stdio.h>
#include <stdbool.h>

int main(){

   bool x;
   x = 10 > 5;

   if(x)
      printf("x is True\n");
   else
      printf("x is False\n");

   bool y;
   int marks = 40;
   y = marks > 50;

   if(y)
      printf("Result: Pass\n");
   else
      printf("Result: Fail\n");
}

运行代码并检查其输出:

Run the code and check its output −

x is True
Result: Fail

Example 3

让我们借助一个 bool 变量实现一个 while 循环 −

Let us implement a while loop with the help of a bool variable −

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int main(void){

   bool loop = true;
   int i = 0;

   while(loop){
      i++;
      printf("i: %d \n", i);

      if (i >= 5)
         loop = false;
   }
   printf("Loop stopped!\n");
   return EXIT_SUCCESS;
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

i: 1
i: 2
i: 3
i: 4
i: 5
Loop stopped!