C/C++實(shí)操True and false詳解
在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)容!
- Python解惑之True和False詳解
- javascript 使用正則test( )第一次是 true,第二次是false
- java 中 request.getSession(true、false、null)的區(qū)別
- 細(xì)數(shù)Ajax請(qǐng)求中的async:false和async:true的差異
- 詳談Ajax請(qǐng)求中的async:false/true的作用(ajax 在外部調(diào)用問(wèn)題)
- javascript中return,return true,return false三者的用法及區(qū)別
- Python返回真假值(True or False)小技巧
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)通訊錄系統(tǒng)課程設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)通訊錄系統(tǒng)課程設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07C++開源庫(kù)nlohmann/json的介紹和使用詳解
nlohmann/json?是一個(gè)C++實(shí)現(xiàn)的JSON解析器,使用非常方便直觀,這篇文章主要為大家詳細(xì)介紹了nlohmann/json的簡(jiǎn)介和使用,需要的可以參考下2023-12-12使用用C++做一顆會(huì)跳動(dòng)的愛心實(shí)例代碼
大家好,本篇文章主要講的是使用用C++做一顆會(huì)跳動(dòng)的愛心實(shí)例代碼,感興趣的同學(xué)趕快來(lái)看一看吧,歡迎借鑒學(xué)習(xí)C++做一顆會(huì)跳動(dòng)的愛心實(shí)例代碼2021-12-12C++使用CriticalSection實(shí)現(xiàn)線程同步實(shí)例
這篇文章主要介紹了C++使用CriticalSection實(shí)現(xiàn)線程同步實(shí)例,是使用CriticalSection對(duì)前文實(shí)例的擴(kuò)展,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-10-10使用VC6.0對(duì)C語(yǔ)言程序進(jìn)行調(diào)試的基本手段分享
這篇文章主要介紹了用VC6.0開發(fā)c語(yǔ)言程序的時(shí)候調(diào)試代碼的一些小技巧,需要的朋友可以參考下2013-07-07C++智能指針shared_ptr與weak_ptr的實(shí)現(xiàn)分析
shared_ptr是一個(gè)標(biāo)準(zhǔn)的共享所有權(quán)的智能指針,允許多個(gè)指針指向同一個(gè)對(duì)象,定義在 memory 文件中,命名空間為 std,這篇文章主要介紹了C++ 中 shared_ptr weak_ptr,需要的朋友可以參考下2022-09-09C++中實(shí)現(xiàn)線程安全和延遲執(zhí)行詳解
這篇文章主要為大家詳細(xì)介紹了C++中實(shí)現(xiàn)線程安全和延遲執(zhí)行的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的小伙伴可以了解下2024-01-01