c++中各种数据类型所占字节

求各种数据类型所占用的字节数可调用sizeof函数,求各种数据类型的最大值可以调用limits标准库中的numeric_limits<T>::max(),numeric_limits<T>::min()函数
#include<iostream>
#include<string>
#include <limits>
using namespace std;

int main()
{
	cout << "type: \t\t" << "************size**************" << endl;
	cout << "bool: \t\t" << "所占字节数:" << sizeof(bool);
	cout << "\t最大值:" << (numeric_limits<bool>::max)();
	cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl;
	cout << "char: \t\t" << "所占字节数:" << sizeof(char);
	cout << "\t最大值:" << (numeric_limits<char>::max)();
	cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl;
	cout << "signed char: \t" << "所占字节数:" << sizeof(signed char);
	cout << "\t最大值:" << (numeric_limits<signed char>::max)();
	cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl;
	cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char);
	cout << "\t最大值:" << (numeric_limits<unsigned char>::max)();
	cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl;
	cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t);
	cout << "\t最大值:" << (numeric_limits<wchar_t>::max)();
	cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;
	cout << "short: \t\t" << "所占字节数:" << sizeof(short);
	cout << "\t最大值:" << (numeric_limits<short>::max)();
	cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;
	cout << "int: \t\t" << "所占字节数:" << sizeof(int);
	cout << "\t最大值:" << (numeric_limits<int>::max)();
	cout << "\t最小值:" << (numeric_limits<int>::min)() << endl;
	cout << "unsigned: \t" << "所占字节数:" << sizeof(unsigned);
	cout << "\t最大值:" << (numeric_limits<unsigned>::max)();
	cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl;
	cout << "long: \t\t" << "所占字节数:" << sizeof(long);
	cout << "\t最大值:" << (numeric_limits<long>::max)();
	cout << "\t最小值:" << (numeric_limits<long>::min)() << endl;
	cout << "unsigned long: \t" << "所占字节数:" << sizeof(unsigned long);
	cout << "\t最大值:" << (numeric_limits<unsigned long>::max)();
	cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl;
	cout << "double: \t" << "所占字节数:" << sizeof(double);
	cout << "\t最大值:" << (numeric_limits<double>::max)();
	cout << "\t最小值:" << (numeric_limits<double>::min)() << endl;
	cout << "long double: \t" << "所占字节数:" << sizeof(long double);
	cout << "\t最大值:" << (numeric_limits<long double>::max)();
	cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl;
	cout << "float: \t\t" << "所占字节数:" << sizeof(float);
	cout << "\t最大值:" << (numeric_limits<float>::max)();
	cout << "\t最小值:" << (numeric_limits<float>::min)() << endl;
	cout << "size_t: \t" << "所占字节数:" << sizeof(size_t);
	cout << "\t最大值:" << (numeric_limits<size_t>::max)();
	cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl;
	cout << "string: \t" << "所占字节数:" << sizeof(string) << endl;
	// << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl;
	cout << "type: \t\t" << "************size**************" << endl;
	system("pause");
	return 0;
}
时间: 2025-01-04 01:18:50

c++中各种数据类型所占字节的相关文章

C/C++基本数据类型所占字节数

关于这个基本的问题,很早以前就很清楚了,C标准中并没有具体给出规定那个基本类型应该是多少字节数,而且这个也与机器.OS.编译器有关,比如同样是在32bits的操作系统系,VC++的编译器下int类型为占4个字节:而tuborC下则是2个字节. 所以int,long int,short int的宽度都可能随编译器而异.但有几条铁定的原则(ANSI/ISO制订的): sizeof(short int)<=sizeof(int) sizeof(int)<=sizeof(long int) short

java基本数据类型所占字节数

JAVA基本数据类型所占字节数是多少?(32位系统) byte     1字节 short    2字节 int      4字节 long     8字节 char     2字节(C语言中是1字节)可以存储一个汉字 float    4字节 double   8字节 boolean  false/true(理论上占用1bit,1/8字节,实际处理按1byte处理) JAVA是采用Unicode编码.每一个字节占8位.你电脑系统应该是 32位系统,这样每个int就是 4个字节 其中一个字节由8

10055 - Hashmat the Brave Warrior &amp; 各数据类型所占字节数 (C语言)

Problem A Hashmat the brave warrior Input: standard input Output: standard output Hashmat is a brave warrior who with his group of young soldiers moves from one place to another to fight against his opponents. Before fighting he just calculates one t

转:C/C++基本数据类型所占字节数

参考:http://blog.csdn.net/vast_sea/article/details/8076934 关于这个基本的问题,很早以前就很清楚了,C标准中并没有具体给出规定那个基本类型应该是多少字节数,而且这个也与机器.OS.编译器有关,比如同样是在32bits的操作系统系,VC++的编译器下int类型为占4个字节:而tuborC下则是2个字节. 所以int,long int,short int的宽度都可能随编译器而异.但有几条铁定的原则(ANSI/ISO制订的): sizeof(sho

C、C++数据类型所占字节数

C标准中并没有详细给出规定那个基本类型应该是多少字节数.详细与机器.OS.编译器有关,比方相同是在32bits的操作系统系,VC++的编译器下int类型为占4个字节:而tuborC下则是2个字节. 所以int,long int,short int的宽度都可能随编译器而异.但有几条铁定的原则(ANSI/ISO制订的): sizeof(short int)<=sizeof(int) sizeof(int)<=sizeof(long int) short int至少应为16位(2字节) long i

JAVA基本数据类型所占字节数是多少?

byte     1字节 short    2字节 int      4字节 long     8字节 char     2字节(C语言中是1字节)可以存储一个汉字 float    4字节 double   8字节 boolean  false/true(理论上占用1bit,1/8字节,实际处理按1byte处理) JAVA是采用Unicode编码.每一个字节占8位.你电脑系统应该是 32位系统,这样每个int就是 4个字节 其中一个字节由8个二进制位组成 Java一共有8种基本数据类型(原始数

查看Java中每个数据类型所占的位数,和表示数值的范围,并得出结论。

java中有8种基本数据类型:byte.int.short.long.boolean.char.float.double 对应的类为:Byte.Int.Short.Long.Boolean.Charecter.Float.Double 逻辑型:boolean 文本型:char 整数型:byte.short.int.long 浮点型:float.double byte:    1字节 -128~127 short:   2字节 -2^15~2^15-1 int :      4字节 -2^31~2

C++主要数据类型在计算机中所占字节大小

遇到了数据存储的大端和小端问题,这你妹的看的一头雾水,发现我基本知识严重匮乏啊,先了解C++各数据类型在自己机子上占多少字节吧,以及这些数据类型所占字节大小与神马有关.各种查资料然后写代码检验,小结于本文中,给自己补补基础知识. 先来列一下C++的数据类型: 整型:int,long.这两个还分signed(有符号)和unsigned(无符号)的,当然有符号和无符号所占字节大小是一样的,就不管他了. 布尔型:bool 字符型:char,wchar_t 浮点型:float,double 这只能上代码

Java中基本数据类型的对比记忆

Java中八种基本类型数据情况: 数据类型 所占字节数 所占位数(二进制位数) 可表示范围 默认值 包装类 备注 byte(字节) 1 8 -128 - 127  0  Byte   short(短整型) 2 16 -32768 - 32768   0  Short    int(整型) 4 32 -2147483648-2147483648   0 Integer    long(长整型) 8 64 -9233372036854477808-9233372036854477808  0  Lo