关于C#:C99布尔数据类型?

C99 boolean data type?

c99布尔数据类型是什么?如何使用它?


包括表头

1
2
3
4
5
#include <stdbool.h>

int main(void){
  bool b = false;
}

truefalse分别扩展到10

7.16布尔型和值< stdbool.h >

  • 1 The header de?nes four macros.
  • 2 The macro

    • bool
      expands to _Bool.
  • 3 The remaining three macros are suitable for use in #if preprocessing directives. They
    are

    • true : which expands to the integer constant 1,
    • false: which expands to the integer constant 0, and
    • __bool_true_false_are_defined
      which expands to the integer constant 1.
  • 4 Notwithstanding the provisions of 7.1.3, a program may unde?ne and perhaps then
    rede?ne the macros bool, true, and false.


请查看在DaniWeb上找到的这个相关线程的答案。

为方便参考,此处摘录并引用:

c99中新关键词的使用

_Bool: C99's boolean type. Using _Bool directly is only recommended if you're
maintaining legacy code that already
defines macros for bool, true, or
false. Otherwise, those macros are
standardized in the
header. Include that header and you
can use bool just like you would in
C++.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <stdbool.h>

int main ( void )
{
  bool b = true;

  if ( b )
    printf ("Yes
"
);
  else
    printf ("No
"
);

  return 0;
}