热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C++实操Trueandfalse

原标题:C++实操-Trueandfalse在C11标准文档中,规定了关系运算符<、>、<=、>

原标题: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("Hel文章来源地址51593.htmllo world!\n");
return 0;
}
$ gcc -o tof tof.c
$ ./tof
varA:1.
varA:1.
varA:0.
Hello world!

www.yii666.com






为了更方便程序员对布尔类型的使用,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 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 Pubwww.yii666.comlic 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
.*/
/*
* 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 */
/* Supporting in 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
$ ./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。


如下程序所示:



#include
int main()
{
bool varA;
printf("文章来源地址51593.htmlfalse:%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 文章来源站点https://www.yii666.com/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虚拟机下编辑编译的示例代码。



来源于:C++实操 - True and false


推荐阅读
author-avatar
Me丶i丶晓莹丬_690
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有