当前位置:首页 > 通信资讯 > 正文

c++中true和false(c++true和false)

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

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

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

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

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

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

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

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

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

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

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

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

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

使用了stdbool.h的C程序:

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

false是0,true是1。

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

注意:

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

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

总结

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

原文链接:https://blog.csdn.net/guoqx/article/details/120497354

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。