c++中,bool与int 的区别

菜鸟一枚,为了观察区别,特地运行了下面几个语句

 1 /*阅读程序回答问题,
 2 1.bool类型的false对应数值?true呢?
 3 2.非0整数对应bool型的?0呢?
 4  */
 5 #include<iostream>
 6 #include<cstring>
 7 using namespace std;
 8 int main(){
 9     cout<<(2>1)<<(2==1)<<endl;
10      bool b=1<2;cout<<b<<endl;
11      bool c=3;cout<<c<<endl;
12     bool d=-3;cout<<d<<endl;
13      int e=3;cout<<e<<endl;
14      bool f=1;cout<<sizeof(f)<<endl;
15      if (5) cout<<"5  true"<<endl;
16      if (-5) cout<<"-5  true"<<endl;
17      if (0) cout<<"0 is ?"<<endl;
18      return 0;
19 }

输出结果:

10

1

1

1

3

1

5 true

-5 true

收获:bool类型占一个字节,对于任何非0整数都代表true,只有0代表false;

时间: 2024-07-31 14:34:59

c++中,bool与int 的区别的相关文章

C#中bool和Boolean的区别

简单介绍下Boolean和bool的区别: 1.bool是基本值类型,Boolean是对象. 2.bool是Boolean的别名,bool是C#中的,Boolean是.net Framework中的. MSDN中解释bool与Boolean的关系是:bool关键字是System.Boolean的别名.实际使用无任何差别.它们的成员也是一样的. 最后总结:bool就像你的乳名,爹妈及内部所属成员都认识,Boolean就像你的学名.大家都认识.不过不管按照乳名还是学名称呼你,最终目的是一样的,称呼的

c++ 中 BOOL与bool TRUE与true FALSE与false 区别 (转载)

http://blog.chinaunix.net/uid-28458801-id-3941112.html FALSE/TRUE与false/true的区别 1.FALSE/TRUE与false/true的区别: false/true是标准C++语言里新增的关键字,而FALSE/TRUE是通过#define,这要用途 是解决程序在C与C++中环境的差异,以下是FALSE/TRUE在windef.h的定义: #ifndef FALSE #define FALSE 0 #endif #ifndef

( 转)Sqlserver中tinyint, smallint, int, bigint的区别 及 10进制转换16进制的方法

一.类型比较 bigint:从-2^63(-9223372036854775808)到2^63-1(9223372036854775807)的整型数据,存储大小为 8 个字节.一个字节就是8位,那么bigint就有64位 int:从-2^31(-2,147,483,648)到2^31-1(2,147,483,647)的整型数据,存储大小为 4 个字节.int类型,最大可以存储32位的数据 smallint:从-2^15(-32,768)到2^15-1(32,767)的整数数据,存储大小为 2 个

mysql中timestamp,datetime,int类型的区别与优劣

mysql中timestamp,datetime,int类型的区别与优劣 int 1. 占用4个字节 2. 建立索引之后,查询速度快 3. 条件范围搜索可以使用使用between 4. 不能使用mysql提供的时间函数 结论:适合需要进行大量时间范围查询的数据表 datetime 1. 占用8个字节 2. 允许为空值,可以自定义值,系统不会自动修改其值. 3. 实际格式储存(Just stores what you have stored and retrieves the same thing

Android - Context中的getText(int resId)方法和getString(int resId)方法的区别

Android开发中,经常在Activity中使用getText(int resId)和getString(int resId)这两个方法,那么这两个方法有什么区别和联系呢? 这两个方法的参数都是资源ID,区别在于getText(int resId)返回的是一个CharSequence,而getString(int resId)返回的是一个String.源代码如下: getText(int resId): /** * Return a localized, styled CharSequence

.NET中struct与class的区别

在.net中的struct与class有很多相似之处,比如可以直接new,对于成员可以直接XX.field,以至于有不少程序员在用时,将其混在一起,分不清有何区别.这两者有何区别呢? 1.类型不同 我们先来看一段代码 static void Main(string[] args) { TypeDemo(); Console.ReadLine(); } // Reference type (because of 'class') class SomeClassRef { public Int32

C#中结构与类的区别

一.类与结构的示例比较: 结构示例: 1 public struct Person 2 { 3 string Name; 4 int height; 5 int weight 6 public bool overWeight() 7 { 8 //implement something 9 } 10 } 类示例: 1 public class TestTime 2 { 3 int hours; 4 int minutes; 5 int seconds; 6 7 public void passti

浅谈MFC中BitBlt与StretchDIBits的区别

一.基础知识 1.BitBlt BitBlt 用于从原设备中复制位图到目标设备,语法格式如下: BOOL BitBlt( HDC hdcDest, // handle to destination DC int nXDest, // 目标矩形区域的左上角x轴坐标点. int nYDest, // 目标矩形区域的左上角y轴坐标点. int nWidth, // 在目标设备中绘制位图的宽度. int nHeight, // 在目标设备中绘制位图的高度. HDC hdcSrc, // 源设备上下文对象

浅谈C#中堆和栈的区别(附上图解)

线程堆栈:简称栈 Stack 托管堆: 简称堆 Heap 使用.Net框架开发程序的时候,我们无需关心内存分配问题,因为有GC这个大管家给我们料理一切.如果我们写出如下两段代码: 代码段1: public int AddFive(int pValue) { int result; result = pValue + 5; return result; } 代码段2: public class MyInt { public int MyValue; } public MyInt AddFive(i