日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

c 语言bool 类型数据_C ++中的bool数据类型

發布時間:2025/3/11 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c 语言bool 类型数据_C ++中的bool数据类型 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

c 語言bool 類型數據

In C++ programming language, to deal with the Boolean values – C++ added the feature of the bool data type. A bool variable stores either true (1) or false (0) values.

在C ++編程語言中,為了處理布爾值– C ++添加了bool數據類型的功能 。 布爾變量存儲true ( 1 )或false ( 0 )值。

Note that, In C++, true and false are the inbuilt keywords and they represent 1 and 0 respectively.

請注意,在C ++中, true和false是內置關鍵字,它們分別表示1和0。

So, whenever we need to work with such variables in which we have to store only two values i.e. the variable to hold status like, ON/OFF, YES/NO, TRUE/FALSE, etc we can use bool type variable.

因此,每當需要使用這樣的變量時,我們只需要存儲兩個值即可,即要保持狀態的變量,例如ON / OFF,YES / NO,TRUE / FALSE等,我們可以使用bool類型變量 。

Syntax:

句法:

bool variable_name;

Example 1:

范例1:

#include <iostream> using namespace std;int main() {bool var1 = true;bool var2 = false;bool var3 = 1;bool var4 = 0;//printing the valuescout << "var1 : " << var1 << endl;cout << "var2 : " << var2 << endl;cout << "var3 : " << var3 << endl;cout << "var4 : " << var4 << endl;return 0; }

Output:

輸出:

var1 : 1 var2 : 0 var3 : 1 var4 : 0

Example 2:

范例2:

#include <iostream> using namespace std;int main() {bool status = true;if (status)cout << "It's true..." << endl;elsecout << "It's false..." << endl;status = false;if (status)cout << "It's true..." << endl;elsecout << "It's false..." << endl;return 0; }

Output:

輸出:

It's true... It's false...

Note: Any non-zero value considers as true and zero considers as false.

注意:任何非零值均視為true , 零則視為false 。

Example 3:

范例3:

#include <iostream> using namespace std;int main() {bool x = true;cout << "x : " << x << endl;x = -1;cout << "x : " << x << endl;x = -123.45f;cout << "x : " << x << endl;x = "Hello";cout << "x : " << x << endl;x = 123.456f;cout << "x : " << x << endl;x = 0;cout << "x : " << x << endl;x = NULL;cout << "x : " << x << endl;return 0; }

Output:

輸出:

x : 1 x : 1 x : 1 x : 1 x : 1 x : 0 x : 0

Also read: Use of bool in C language

另請閱讀: 在C語言中使用bool

翻譯自: https://www.includehelp.com/cpp-tutorial/bool-data-type-in-cpp.aspx

c 語言bool 類型數據

總結

以上是生活随笔為你收集整理的c 语言bool 类型数据_C ++中的bool数据类型的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。