温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C/C++实操True and false的示例分析

发布时间:2021-09-27 10:43:54 来源:亿速云 阅读:154 作者:小新 栏目:开发技术

这篇文章主要介绍了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 <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

/* 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程序:

#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。

如下程序所示:

#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虚拟机下编辑编译的示例代码。

感谢你能够认真阅读完这篇文章,希望小编分享的“C/C++实操True and false的示例分析”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI