C++基本数据类型
C++基本数据类型
C++定义了⼀套包括算术类型和空类型在内的基本数据类型。其中算术类型包括字符、整型数、布尔值和浮点数;空类型不对应具体的值。算术类型的尺⼨(数据所占的⽐特数)在不同的机器上有所差别,尺⼨越⼤所能表⽰的数据范围就越⼤,但 C++标准规定了最⼩的尺⼨值⼀. 基本内置类型
1. 算数类型
算数类型分为两类:整形(包括字符类型、布尔类型)和浮点型
1. 整型
短整型:short(最⼩尺⼨:16位)整型:int(最⼩尺⼨:16位,⾄少和short⼀样长)
长整型:long (最⼩尺⼨:32位,且⾄少和int⼀样长)长整型:long long(最⼩尺⼨:64位,且⾄少和long⼀样长)
⽆符号型:默认是带符号的。⽆符号数只能表⽰⾮负数,带符号数在可表⽰的范围内可表⽰整个整数范围。unsigned short、
unsigned int、unsigned long、unsigned long long。注意,unsigned本⾝是unsigned int的缩写
表⽰不同尺⼨的整数。C++语⾔中规定了各个整型数类型的尺⼨⼤⼩:short <= int <= long <=long long。
2. 浮点数
单精度浮点数:float(最⼩尺⼨:6位有效数字)
双精度浮点数:double(最⼩尺⼨:10位有效数字)
扩展精度浮点数:long double (最⼩尺⼨:10位有效数字)
通常⼀个 float 型的数据以⼀个word(32 bit)来表⽰,double 通常2个 word 来表⽰,⽽ long double 以3或者4个 word来表⽰。
3. 字符
字符:char(最⼩尺⼨:8位),最多只能包含256种字符
宽字符:wchar_t(最⼩尺⼨:16位),⼀般为16或32位
Unicode字符:char16_t(最⼩尺⼨:16位)
Unicode字符:char32_t(最⼩尺⼨:32位)
char是基本的字符类型,⼀个char的⼤⼩和⼀个机器字节⼀样⼤。wchar_t类型⽤于存放机器最⼤扩展字符集中的任意⼀个字
符,⽽ char32_t 和 char16_t 则为Unicode字符集服务。
字符型被分为三种:char、signed char、unsigned char,其中类型char和signed char并不相同。三种字符型在实际的情况中表现为两种:带符号和不带符号,类型char会表现出其中的⼀种,具体情况由编译器的选择决定。
4. 布尔值
布尔类型:bool(最⼩尺⼨:未定义)
布尔类型的取值为真(True)和假(False)两种。
2. 空类型(void)
空类型没有具体值,⼀般只在特殊场合使⽤,可以当作函数的返回类型。
⼆. 前缀后缀
1. 前缀:
0开头表⽰8进制,0x或0X开头表⽰16进制。
2. 后缀:
l或L表⽰long常量
u或U表⽰unsigned int常量
ul、uL、Ul、UL、lU、lu、LU、Lu表⽰unsigned long常量。(lu可以采⽤任意顺序和⼤⼩写表⽰)
ll、LL表⽰long long 常量。
ull、Ull、uLL、ULL表⽰unsigned long long常量。
3. 对于浮点常量,
f或F表⽰float
l或L表⽰long double
其他都表⽰double
4. 不带后缀时的规则——尽可能采⽤⼩的类型:
对于10进制:int->long->long long
对于8进制或16进制:int->unsigned int->long->unsigned long->long long ->unsigned long long
三. 类型⼤⼩和范围
Data Type Size (in bytes)Range
short int2-32,768 to 32,767(-215~~215-1)
unsigned short int20 to 65,535(0~2^16-1)
unsigned int40 to 4,294,967,295(0~2^32-1)
int4-2,147,483,648 to 2,147,483,647(-231~231-1)
long int4-2,147,483,648 to 2,147,483,647(-231~231-1)
unsigned long int40 to 4,294,967,295(0~2^32-1)
long long int8-(2^63) to (2^63)-1
unsigned long long int80 to 18,446,744,073,709,551,615(0~2^64-1)
signed char1-128 to 127(-28~28-1)
unsigned char10 to 255(0~2^8-1)
float4
double8
long double12
wchar_t 2 or 4 1 wide character
float数值范围
Type Name Bytes Other Names Range of Values
int4signed-2,147,483,648 to 2,147,483,647
unsigned int4unsigned0 to 4,294,967,295
__int81char-128 to 127
unsigned __int81unsigned char0 to 255
__int162short, short int, signed short int-32,768 to 32,767
unsigned __int162unsigned short, unsigned short int0 to 65,535
__int324signed, signed int, int-2,147,483,648 to 2,147,483,647
unsigned __int324unsigned, unsigned int0 to 4,294,967,295
__int648long long, signed long long-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 unsigned __int648unsigned long long0 to 18,446,744,073,709,551,615
bool1none false or true
char1none-128 to 127 by default 0 to 255 when compiled by using signed char1none-128 to 127
unsigned char1none0 to 255
short2short int, signed short int-32,768 to 32,767
unsigned short2unsigned short int0 to 65,535
long4long int, signed long int-2,147,483,648 to 2,147,483,647
unsigned long4unsigned long int0 to 4,294,967,295
long long8none (but equivalent to __int64)-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 unsigned long long8none (but equivalent to unsigned __int64)0 to 18,446,744,073,709,551,615
enum varies none
float4none 3.4E +/- 38 (7 digits)
double8none 1.7E +/- 308 (15 digits)
long double same as double none Same as double
wchar_t2__wchar_t0 to 65,535
Type Name Bytes Other Names Range of Values
类型位范围
char 1 个字节-128 到 127 或者 0 到 255
unsigned char 1 个字节0 到 255
signed char 1 个字节-128 到 127
int 4 个字节-2147483648 到 2147483647
unsigned int 4 个字节0 到 4294967295
signed int 4 个字节-2147483648 到 2147483647
short int 2 个字节-32768 到 32767
unsigned short int2 个字节0 到 65,535
signed short int 2 个字节-32768 到 32767
long int8 个字节-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
signed long int8 个字节-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 unsigned long int8 个字节0 到 18,446,744,073,709,551,615
float 4 个字节精度型占4个字节(32位)内存空间,+/- 3.4e +/- 38 (~7 个数字) double8 个字节双精度型占8 个字节(64位)内存空间,+/- 1.7e +/- 308 (~15 个数字) long double16 个字节长双精度型 16 个字节(128位)内存空间,可提供18-19位有效数字。wchar_t 2 或 4 个字节1 个宽字符
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论