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常量。
  1. 对于浮点常量,
  • f或F表示float

  • l或L表示long double

  • 其他都表示double

  1. 不带后缀时的规则——尽可能采用小的类型:
  • 对于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 int 2 -32,768 to 32,767(-215~~215-1)
unsigned short int 2 0 to 65,535(0~2^16-1)
unsigned int 4 0 to 4,294,967,295(0~2^32-1)
int 4 -2,147,483,648 to 2,147,483,647(-231~231-1)
long int 4 -2,147,483,648 to 2,147,483,647(-231~231-1)
unsigned long int 4 0 to 4,294,967,295(0~2^32-1)
long long int 8 -(2^63) to (2^63)-1
unsigned long long int 8 0 to 18,446,744,073,709,551,615(0~2^64-1)
signed char 1 -128 to 127(-28~28-1)
unsigned char 1 0 to 255(0~2^8-1)
float 4
double 8
long double 12
wchar_t 2 or 4 1 wide character

Type Name Bytes Other Names Range of Values
int 4 signed -2,147,483,648 to 2,147,483,647
unsigned int 4 unsigned 0 to 4,294,967,295
__int8 1 char -128 to 127
unsigned __int8 1 unsigned char 0 to 255
__int16 2 short, short int, signed short int -32,768 to 32,767
unsigned __int16 2 unsigned short, unsigned short int 0 to 65,535
__int32 4 signed, signed int, int -2,147,483,648 to 2,147,483,647
unsigned __int32 4 unsigned, unsigned int 0 to 4,294,967,295
__int64 8 long long, signed long long -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned __int64 8 unsigned long long 0 to 18,446,744,073,709,551,615
bool 1 none false or true
char 1 none -128 to 127 by default 0 to 255 when compiled by using /J
signed char 1 none -128 to 127
unsigned char 1 none 0 to 255
short 2 short int, signed short int -32,768 to 32,767
unsigned short 2 unsigned short int 0 to 65,535
long 4 long int, signed long int -2,147,483,648 to 2,147,483,647
unsigned long 4 unsigned long int 0 to 4,294,967,295
long long 8 none (but equivalent to __int64) -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned long long 8 none (but equivalent to unsigned __int64) 0 to 18,446,744,073,709,551,615
enum varies none
float 4 none 3.4E +/- 38 (7 digits)
double 8 none 1.7E +/- 308 (15 digits)
long double same as double none Same as double
wchar_t 2 __wchar_t 0 to 65,535
类型 范围
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 int 2 个字节 0 到 65,535
signed short int 2 个字节 -32768 到 32767
long int 8 个字节 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
signed long int 8 个字节 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
unsigned long int 8 个字节 0 到 18,446,744,073,709,551,615
float 4 个字节 精度型占4个字节(32位)内存空间,+/- 3.4e +/- 38 (~7 个数字)
double 8 个字节 双精度型占8 个字节(64位)内存空间,+/- 1.7e +/- 308 (~15 个数字)
long double 16 个字节 长双精度型 16 个字节(128位)内存空间,可提供18-19位有效数字。
wchar_t 2 或 4 个字节 1 个宽字符
posted @ 2021-03-31 15:34  风间Komorebi  阅读(1380)  评论(0)    收藏  举报