欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C/C++實(shí)操True and false詳解

 更新時(shí)間:2021年09月27日 10:13:25   作者:夜流冰  
這篇文章主要給大家介紹了關(guān)于Python中常用的數(shù)據(jù)類型bool(布爾)類型的兩個(gè)值:True和False的相關(guān)資料,通過(guò)示例代碼給大家進(jìn)行了解惑,讓對(duì)這兩個(gè)值有所疑惑的朋友們能有起到一定的幫助,需要的朋友下面來(lái)一起看看吧。

在C11標(biāo)準(zhǔn)文檔中,規(guī)定了關(guān)系運(yùn)算符 <、> 、<= 、>=的運(yùn)算結(jié)果,真時(shí)返回1,假時(shí)返回0,返回類型為整型。

運(yùn)算符==、!=和關(guān)系運(yùn)算符類似,除了運(yùn)算優(yōu)先級(jí)較低以外,也是返回1或0。

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

C語(yǔ)言本身只有一個(gè)_Bool定義,是一個(gè)關(guān)鍵字。

_Bool類型是一個(gè)對(duì)象,存儲(chǔ)0和1兩個(gè)值,是一個(gè)無(wú)符號(hào)的整型。

如下程序所示,_Bool只有0和1,即假和真兩個(gè)值,賦值時(shí)非0都看作1。

任何一個(gè)標(biāo)量值給_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!

為了更方便程序員對(duì)布爾類型的使用,C語(yǔ)言的標(biāo)準(zhǔn)庫(kù),頭文件<stdbool.h>,定義了和布爾操作相關(guān)的類型。 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類型,其實(shí)就是_Bool。

并定義了true為1,false為0,方便使用。

這幾個(gè)宏按照上面的定義展開為類型_Bool以及常數(shù)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!

同時(shí)我們看到了stdbool.h里面還使用了__cplusplus這個(gè)C++編譯器的宏開關(guān),如果使用C++編譯器來(lái)編譯C程序時(shí),就是用下面的宏定義。

這時(shí)定義了4個(gè),bool、false、和true都原封不動(dòng),說(shuō)明C++語(yǔ)言本身自帶定義。而_Bool轉(zhuǎn)換為bool,表明C++里沒(méi)有_Bool,轉(zhuǎn)而使用bool。

下面我們來(lái)看一下C++里面的true、false的定義:

查看C++11標(biāo)準(zhǔn)文檔,C++里bool、true、false都是關(guān)鍵字。

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,關(guān)于大寫的TRUE和FALSE定義,在C/C++語(yǔ)言和標(biāo)準(zhǔn)庫(kù)里都沒(méi)有定義,程序中使用的都是單獨(dú)添加的。

2,本文使用的gcc版本:gcc version 9.3.0,Ubuntu虛擬機(jī)下編輯編譯的示例代碼。

總結(jié)

本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

最新評(píng)論