首页 > 编程语言 >C++基本数据类型的大小和取值范围

C++基本数据类型的大小和取值范围

时间:2023-06-27 17:57:32浏览次数:35  
标签:min max 数据类型 long C++ byte 取值 sizeof

bit, byte, word

  1. bit 比特,位

Computers store data as a sequence of bits, each holding a 0 or 1, such as 00011011011...

  1. byte 字节

Most computers deal with memory as chunks of bits of sizes that are powers of 2. The smallest chunk of addressable memory is referred to as a "byte".
...
Most computers associate a number (called an "address") with each byte in memory.

  1. word 字
    由若干个字节组成:
    在16位的系统中(比如8086微机) 1字 (word)= 2字节(byte)= 16(bit)
    在32位的系统中(比如win32) 1字(word)= 4字节(byte)=32(bit)
    在64位的系统中(比如win64)1字(word)= 8字节(byte)=64(bit)

C++中的算术类型

  1. C++中的算术类型(arithmetic types)的数据是指可以进行加减乘除等算术运算的数值型数据(也可以理解为就是“数字(number)”)。
  2. 算术类型又可以分为integral types和floating-point types两类:
  • integral types
    • boolean values: bool
    • characters: char ...
    • integers: short, int, long, long long
  • floating-point types
    • float, double, ...

C++中算术类型的尺寸

  1. C++中算术类型的size(尺寸)指的是该类型的数据在内存中所占的字节数。

  2. C++标准中只对每一种类型的minimum size进行了规定,真实尺寸依赖于编译器的具体实现。
    image

  3. char:1 byte

  4. sizeof(xxx)=sizeof(unsigned xxx)=sizeof(signed xxx)

  5. sizeof(short)<=sizeof(int)<=sizeof(long)<=sizeof(long long)

  6. Typically, floats are represented in one word (32 bits), doubles in two words (64 bits), and long doubles in either three or four words (96 or 128 bits). The float and double types typically yield about 7 and 16 significant digits, respectively.

  7. 类型尺寸依赖于编译器的具体实现,相应的,不同类型的取值范围(最大值和最小值)也依赖于编译器的具体实现。

  8. 查看类型尺寸和类型取值范围的代码:

#include <iostream>
using namespace std;

// 查看int类型所占的字节数
cout << "所占字节数:" << sizeof(int);
// 查看int类型的取值范围
cout << "最大值:" << (numeric_limits<int>::max)();
cout << "最小值:" << (numeric_limits<int>::min)() << endl;
  1. 下面是clang编译器分别在32位和64位机器上的数据类型尺寸:

代码:

#include <iostream>
using namespace std;

int main()
{
    cout << "sizeof(bool)          : " << sizeof(bool) << endl;
    cout << "sizeof(char)          : " << sizeof(char) << endl;
    cout << "sizeof(int)           : " << sizeof(int) << endl;
    cout << "sizeof(unsigned int)  : " << sizeof(unsigned int) << endl;
    cout << "sizeof(short int)     : " << sizeof(short int) << endl;
    cout << "sizeof(long int)      : " << sizeof(long int) << endl;
    cout << "sizeof(float)         : " << sizeof(float) << endl;
    cout << "sizeof(double)        : " << sizeof(double) << endl;

    cout << "min(bool)          : " << numeric_limits<bool>::min() << endl;
    cout << "min(int)           : " << numeric_limits<int>::min() << endl;
    cout << "min(unsigned int)  : " << numeric_limits<unsigned int>::min() << endl;
    cout << "min(short int)     : " << numeric_limits<short int>::min() << endl;
    cout << "min(long int)      : " << numeric_limits<long int>::min() << endl;
    cout << "min(float)         : " << numeric_limits<float>::min() << endl;
    cout << "min(double)        : " << numeric_limits<double>::min() << endl;

    cout << "max(bool)          : " << numeric_limits<bool>::max() << endl;
    cout << "max(int)           : " << numeric_limits<int>::max() << endl;
    cout << "max(unsigned int)  : " << numeric_limits<unsigned int>::max() << endl;
    cout << "max(short int)     : " << numeric_limits<short int>::max() << endl;
    cout << "max(long int)      : " << numeric_limits<long int>::max() << endl;
    cout << "max(float)         : " << numeric_limits<float>::max() << endl;
    cout << "max(double)        : " << numeric_limits<double>::max() << endl;

    return 0;
}

运行结果:
image
总结:
image

(signed) short = INT16_MIN ~ INT16_MAX
(signed) int = INT32_MIN ~ INT32_MAX
(unsigned) int = 0 ~ 2*INT32_MAX
(signed) long = INT64_MIN ~ INT64_MAX

标签:min,max,数据类型,long,C++,byte,取值,sizeof
From: https://www.cnblogs.com/ZhuYuxi/p/17507509.html

相关文章

  • LeetCode C++:HashTable篇
    1、TwoSumGivenanarrayofintegersnums andanintegertarget,returnindicesofthetwonumberssuchthattheyadduptotarget.Youmayassumethateachinputwouldhaveexactlyonesolution,andyoumaynotusethesameelementtwice.Youcanreturn......
  • vscode--C++配置问题
    1、#include<iostream>报红但是能正常运行解决方案:win+Rcmd进入输入gcc-v-E-xc++-将红框中数据复制进随后保存即可......
  • 在Androidstudio 中 通过jni java 和c++相互调用实现方法
    在Androidstudio中通过jnijava和c++相互调用实现方法1.创建javanative方法类publicclassNativeAPI{privatestaticfinalStringTAG=APP_TAG+"TestJni";static{System.loadLibrary("testnative");}//从c++so中获取字符串publicna......
  • C++太难学,怎么破?这本书给你指点迷津!
    2021年在线学习平台Springboard选出了最难学的编程语言TOP5,C++排在其中之一。C++难学的理由很多,比如它语法复杂,语法特性多,编程范式灵活,标准库内容过于基础,还要具备C语言基础等等。提起C++,它是由C语言大幅扩展而成,且用途非常广泛,例如用于Windows等操作系统、文字处理和......
  • C++ 容器比较 - Vector,
    C++容器STL准备了两类7种基本容器类型1.序列式容器:向量(vector)/双端队列(deque)/列表(List)/(string,array当做一种序列式容器)-与插入次序有关2.关联式容器(已序群集)-与插入次序无关(set,multiset,map,multiset)1.vectorvector(向量):是一种序列式容器,事实上和数组差不多,但它......
  • C++面试八股文:什么是智能指针?
    C++面试八股文:什么是智能指针?某日二师兄参加XXX科技公司的C++工程师开发岗位第19面:面试官:什么是智能指针?二师兄:智能指针是C++11引入的类模板,用于管理资源,行为类似于指针,但不需要手动申请、释放资源,所以称为智能指针。面试官:C++11引入了哪些智能指针?二师兄:三种,分别是sh......
  • Python全栈学习 day07 数据类型(三)
    day06数据类型(下)常见的数据类型:int,整数类型(整形)bool,布尔类型str,字符串类型list,列表类型tuple,元组类型dict,字典类型set,集合类型float,浮点类型(浮点型)目标:掌握字典、集合、浮点类型相关知识。课程概要:set集合,一个不允许重复重复&可变类型(元素可哈希)。dict字典,一个......
  • C++ Primer 第一章 开始
    输入输出C++并未定义任何输入输出,取而代之包含了一个标准库提供输入输出。iostream库包含两个基础类型:istream和ostream,分别表示输入流和输出流,流代表字符序列。标准库定义了4个IO对象cin为istream类型对象,也称为标准输入cout为ostream类型对象,也称为标准输出标准库还定义了......
  • C++面试八股文:std::deque用过吗?
    某日二师兄参加XXX科技公司的C++工程师开发岗位第26面:面试官:deque用过吗?二师兄:说实话,很少用,基本没用过。面试官:为什么?二师兄:因为使用它的场景很少,大部分需要性能、且需要自动扩容的时候使用vector,需要随机插入和删除的时候可以使用list。面试官:那你知道STL中的stack是如何实......
  • C++指针函数
    指针函数指针函数有些像C#中的委托delegate(不知道理解的对不对)。定义函数指针int*compare(int,int);一般简写为typedefint(*compare)(int,int);这样就定义了一个名为compare的函数指针。compare指针类型为:指向返回int类型并带有两个int参数的函数的指针。函数指......