C/C++实操True|C/C++实操True and false详解

【C/C++实操True|C/C++实操True and false详解】在C11标准文档中,规定了关系运算符 <、> 、<= 、>=的运算结果,真时返回1,假时返回0,返回类型为整型。
运算符==、!=和关系运算符类似,除了运算优先级较低以外,也是返回1或0。
真(True)的定义是非0,所以假(False)的定义就是整型的0值。
C语言本身只有一个_Bool定义,是一个关键字。
_Bool类型是一个对象,存储0和1两个值,是一个无符号的整型。
如下程序所示,_Bool只有0和1,即假和真两个值,赋值时非0都看作1。
任何一个标量值给_Bool类型变量赋值,如果等于0,赋值为0,否则就赋值为1。

#include 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$ ./tofvarA:1.varA:1.varA:0.Hello world!

为了更方便程序员对布尔类型的使用,C语言的标准库,头文件,定义了和布尔操作相关的类型。 stdbool.h
?/* 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 modifyit under the terms of the GNU General Public License as published bythe 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 ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See theGNU General Public License for more details.Under Section 7 of GPL version 3, you are granted additionalpermissions described in the GCC Runtime Library Exception, version3.1, as published by the Free Software Foundation.You should have received a copy of the GNU General Public License anda copy of the GCC Runtime Library Exception along with this program; see the files COPYING3 and COPYING.RUNTIME respectively.If not, see.*//** ISO C Standard:7.16Boolean type and values*/#ifndef _STDBOOL_H#define _STDBOOL_H#ifndef __cplusplus#define bool_Bool#define true1#define false0#else /* __cplusplus *//* Supportingin C++ is a GCC extension.*/#define _Boolbool#define boolbool#define falsefalse#define truetrue#endif /* __cplusplus *//* Signal that all the definitions are present.*/#define __bool_true_false_are_defined1#endif/* stdbool.h */?

C里的头文件,stdbool.h,定义了bool类型,其实就是_Bool。
并定义了true为1,false为0,方便使用。
这几个宏按照上面的定义展开为类型_Bool以及常数1和0。
使用了stdbool.h的C程序:
#include #include 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$ ./tofvarA: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。
如下程序所示:
#include 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$ ./tofplusfalse: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虚拟机下编辑编译的示例代码。
总结 本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!

    推荐阅读