经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
C/C++实操True and false详解
来源:jb51  时间:2021/9/27 11:05:52  对本文有异议

在C11标准文档中,规定了关系运算符 <、> 、<= 、>=的运算结果,真时返回1,假时返回0,返回类型为整型。

运算符==、!=和关系运算符类似,除了运算优先级较低以外,也是返回1或0。

真(True)的定义是非0,所以假(False)的定义就是整型的0值。

C语言本身只有一个_Bool定义,是一个关键字。

_Bool类型是一个对象,存储0和1两个值,是一个无符号的整型。

如下程序所示,_Bool只有0和1,即假和真两个值,赋值时非0都看作1。

任何一个标量值给_Bool类型变量赋值,如果等于0,赋值为0,否则就赋值为1。

  1. #include <stdio.h>
  2. int main()
  3. {
  4. _Bool varA;
  5. varA = 2;
  6. printf("varA:%d.\n",varA);
  7. varA = -1;
  8. printf("varA:%d.\n",varA);
  9. varA = 0;
  10. printf("varA:%d.\n",varA);
  11. printf("Hello world!\n");
  12. return 0;
  13. }
  14. $ gcc -o tof tof.c
  15. $ ./tof
  16. varA:1.
  17. varA:1.
  18. varA:0.
  19. Hello world!

为了更方便程序员对布尔类型的使用,C语言的标准库,头文件<stdbool.h>,定义了和布尔操作相关的类型。 stdbool.h

  1. /* Copyright (C) 1998, 1999, 2000, 2009 Free Software Foundation, Inc.
  2. This file is part of GCC.
  3. GCC is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. GCC is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. Under Section 7 of GPL version 3, you are granted additional
  12. permissions described in the GCC Runtime Library Exception, version
  13. 3.1, as published by the Free Software Foundation.
  14. You should have received a copy of the GNU General Public License and
  15. a copy of the GCC Runtime Library Exception along with this program;
  16. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  17. <Licenses- GNU Project - Free Software Foundation>. */
  18. /*
  19. * ISO C Standard: 7.16 Boolean type and values <stdbool.h>
  20. */
  21. #ifndef _STDBOOL_H
  22. #define _STDBOOL_H
  23. #ifndef __cplusplus
  24. #define bool _Bool
  25. #define true 1
  26. #define false 0
  27. #else /* __cplusplus */
  28. /* Supporting <stdbool.h> in C++ is a GCC extension. */
  29. #define _Bool bool
  30. #define bool bool
  31. #define false false
  32. #define true true
  33. #endif /* __cplusplus */
  34. /* Signal that all the definitions are present. */
  35. #define __bool_true_false_are_defined 1
  36. #endif /* stdbool.h */

C里的头文件,stdbool.h,定义了bool类型,其实就是_Bool。

并定义了true为1,false为0,方便使用。

这几个宏按照上面的定义展开为类型_Bool以及常数1和0。

使用了stdbool.h的C程序:

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. int main()
  4. {
  5. bool varA;
  6. varA = 2;
  7. printf("varA:%d.\n",varA);
  8. varA = -1;
  9. printf("varA:%d.\n",varA);
  10. varA = 0;
  11. printf("varA:%d.\n",varA);
  12. varA = true;
  13. printf("varA:%d.\n",varA);
  14. varA = false;
  15. printf("varA:%d.\n",varA);
  16. printf("Hello world!\n");
  17. return 0;
  18. }
  19. $ gcc -o tof tof.c
  20. $ ./tof
  21. varA:1.
  22. varA:1.
  23. varA:0.
  24. varA:1.
  25. varA:0.
  26. Hello world!

同时我们看到了stdbool.h里面还使用了__cplusplus这个C++编译器的宏开关,如果使用C++编译器来编译C程序时,就是用下面的宏定义。

这时定义了4个,bool、false、和true都原封不动,说明C++语言本身自带定义。而_Bool转换为bool,表明C++里没有_Bool,转而使用bool。

下面我们来看一下C++里面的true、false的定义:

查看C++11标准文档,C++里bool、true、false都是关键字。

true、false是字面常量,bool类型的变量值是true或者false。

如下程序所示:

  1. #include <stdio.h>
  2. int main()
  3. {
  4. bool varA;
  5. printf("false:%d,true:%d.\n", false, true);
  6. varA = 2;
  7. printf("varA:%d.\n", varA);
  8. varA = -1;
  9. printf("varA:%d.\n", varA);
  10. varA = 0;
  11. printf("varA:%d.\n", varA);
  12. printf("Hello world!\n");
  13. return 0;
  14. }
  15. $ g++ -o tofplus tof.cpp
  16. $ ./tofplus
  17. false:0,true:1.
  18. varA:1.
  19. varA:1.
  20. varA:0.
  21. Hello world!

false是0,true是1。

bool类型变量的值只能是0或1。

注意:

1,关于大写的TRUE和FALSE定义,在C/C++语言和标准库里都没有定义,程序中使用的都是单独添加的。

2,本文使用的gcc版本:gcc version 9.3.0,Ubuntu虚拟机下编辑编译的示例代码。

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注w3xue的更多内容!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号