整型:
int类型:在32/64位系统中都是32位(4字节)或者16位,范围为-2147483648~+2147483647,无符号情况下表示为0~4294967295;
在16位系统中,为16位,-32768~32767,无符号2^16;
short int类型:占用的存储空间可能比int类型小,16位;
long int类型: (32位)占用的空间可能比int类型大;因为C语言只规定short占用的空间不能多于int;long占用的空间不能少于int;
long long int:(C99标准加入),占用空间可能比long大,至少占64位;
unsigned类型,与以上类型结合表示非负数,表示正数范围更大;
浮点型:
float类型:占32位(4字节),8位表示指数的值和符号,24位用于表示非指数部分及符号;至少有6为有效数字;
double类型:占64位(8字节),将多出的32位全部用来表示非指数部分;至少有13位有效数字;
long double类型:满足比double类型更高的精度要求,至少与double类型精度相同;
浮点型常量:默认为double类型的精度;
在浮点数后边加f或F后缀,看作float类型;使用l或L(建议,易区分)使成为long double类型;
char类型(实际上是整型)
C语言规定占用空间为1字节(8位);
_Bool类型
bool类型是一种无符号类型,存储0或1,表示false或true;所占的空间只要可以存储0或1就可以;
可移植类型:stdint.h inttype.h
不同系统会有差别!!!
使用sizeof()函数,查看实际当前系统的类型大小;
#include <iostream>
#include <string>
#include <limits>
using namespace std;
int main()
{
cout << "type: \t\t" << "************size**************"<< endl << endl;
//布尔类型bool
cout << "bool: \t\t" << "所占字节数:" << sizeof(bool);
cout << "\t最大值:" << (numeric_limits<bool>::max());
cout << "\t\t最小值:" << (numeric_limits<bool>::min()) << endl;
//字符类型char
cout << "char: \t\t" << "所占字节数:" << sizeof(char);
cout << "\t最大值:" << (numeric_limits<char>::max());
cout << "\t\t最小值:" << (numeric_limits<char>::min()) << endl;
//宽字符wchar_t
cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t);
cout << "\t最大值:" << (numeric_limits<wchar_t>::max());
cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min()) << endl;
//短整形 short
cout << "short: \t\t" << "所占字节数:" << sizeof(short);
cout << "\t最大值:" << (numeric_limits<short>::max());
cout << "\t\t最小值:" << (numeric_limits<short>::min()) << endl;
//整型 int
cout << "int: \t\t" << "所占字节数:" << sizeof(int);
cout << "\t最大值:" << (numeric_limits<int>::max());
cout << "\t最小值:" << (numeric_limits<int>::min()) << endl;
//长整型 long
cout << "long: \t\t" << "所占字节数:" << sizeof(long);
cout << "\t最大值:" << (numeric_limits<long>::max());
cout << "\t最小值:" << (numeric_limits<long>::min()) << endl;
//双浮点型double
cout << "double: \t" << "所占字节数:" << sizeof(double);
cout << "\t最大值:" << (numeric_limits<double>::max());
cout << "\t最小值:" << (numeric_limits<double>::min()) << endl;
//浮点型 float
cout << "float: \t\t" << "所占字节数:" << sizeof(float);
cout << "\t最大值:" << (numeric_limits<float>::max());
cout << "\t最小值:" << (numeric_limits<float>::min()) << endl;
//size_t
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);
//cout << "\t最大值:" << (numeric_limits<string>::max()) << "\t\t最小值:" << (numeric_limits<string>::min()) << endl;
return 0;
}
欢迎指正!