bool 类型 |
您所在的位置:网站首页 › bool类型函数举例 › bool 类型 |
认识bool类型
C语言里面是没有bool(布尔)类型的,C++里面才有,C语言里面用数值0表示假,非0整数表示真。在C++里面可以使用bool类型。bool类型只有两个值:true =1 、false=0。 bool可用于定义函数类型为布尔型,函数里可以有 return TRUE; return FALSE 之类的语句。bool的内置类型,很好的解决了代码的一致性问题 1 c语言中的bool类型可以自定义为:#define bool int #define false 0 #define true 1 在此之前的C语言中,使用整型int来表示真假。在输入时:使用非零值表示真;零值表示假。在输出时:真的结果是1,假的结果是0。 2使用stdbool.h在C++中,通过bool来定义布尔变量,通过true和false对布尔变量进行赋值。C99为了让我们能够写出与C++兼容的代码 ,添加了一个头文件。 /* Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc. This file is part of GCC. */ #ifndef _STDBOOL_H #define _STDBOOL_H #ifndef __cplusplus #define bool _Bool #define true 1 #define false 0 #else /* __cplusplus ,应用于C++里,这里不用处理它*/ /* Supporting in C++ is a GCC extension. */ #define _Bool bool #define bool bool #define false false #define true true #endif /* __cplusplus */ /* Signal that all the definitions are present. */ #define __bool_true_false_are_defined 1 #endif /* stdbool.h */ 可见,stdbool.h中定义了4个宏,bool、true、false、__bool_true_false_are_defined。 其中bool就是 _Bool类型,true和false的值为1和0,__bool_true_false_are_defined的值为1。下面是一个例子程序: ```c #include #include #include /* 测试C99新添加的头文件 stdbool.h */ int main(){ bool m = true; bool n = false; printf("m==%d, n==%d /n", m, n); printf("sizeof(_Bool) == %d /n", sizeof(_Bool)); system("pause"); return EXIT_SUCCESS; } 执行结果为: m==1, n==0 sizeof(_Bool) == 1转载于 (https://blog.csdn.net/daheiantian/article/details/6241893) |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |