【C++】C++中的基本内置类型

时间:2023-03-08 20:59:49
【C++】C++中的基本内置类型

基本数据类型

下面这张表是C++支持的基本数据类型

类型 含义 最小尺寸
bool 布尔 未定义
char 字符 8位
wchar_t 宽字符 16位
char16_t Unicode字符 16位
char32_t Unicode字符 32位
short 短整型 16位
int 整型 16位
long 长整型 32位
long long 长整型 64位
float 单精度浮点数 6位有效数
double 双精度浮点数 10位有效数字
long double 扩展精度浮点数 10位有效数字

C++语言规定,一个int至少和一个short一样大,一个long至少和一个int一样大,一个long long至少和一个long一样大。其中,long long是C++11新定义的类型。

通常short类型占16位(16比特),那么它的范围就是(-32768~32767)。

通常int类型占32位(1个字,32比特),那么它的范围就是(-2^31~2^31-1)。

通常long类型占64位(64比特),那么它的范围就是(-2^63 ~ 2^63-1)。

通常float类型占32位(1个字,32比特),它的精度为7位。

通常double类型占64位(2个字,64比特),它的精度为15位。

#include <iostream>
#include <limits> using namespace std; int main(){
cout << "type:char\n";
cout << "bytes = " << sizeof(char) << "\n\n"; cout << "type:short\n";
cout << "bytes = " << sizeof(short) << "\n";
cout << "smallest value = " << numeric_limits<short>::min() << "\n";
cout << "largest value = " << numeric_limits<short>::max() << "\n\n"; cout << "type:int\n";
cout << "bytes = " << sizeof(int) << "\n";
cout << "smallest value = " << numeric_limits<int>::min() << "\n";
cout << "largest value = " << numeric_limits<int>::max() << "\n\n"; cout << "type:long";
cout << "bytes = " << sizeof(long) << "\n";
cout << "smallest value = " << numeric_limits<long>::min() << "\n";
cout << "largest value = " << numeric_limits<long>::max() << endl;
return ;
}

输出结果为:

type:char
bytes = 1 type:short
bytes = 2
smallest value = -32768
largest value = 32767 type:int
bytes = 4
smallest value = -2147483648
largest value = 2147483647 type:longbytes = 8
smallest value = -9223372036854775808
largest value = 9223372036854775807

除了上面的这些基本数据类型,其中整型可以划分为带符号的(signed)和无符号的(unsigned)两种。带符号的可以表示整数、负数和0,无符号的只能表示非负数。

short、int、long、long long表示有符号的,在它们的前面加上unsigned表示无符号的:unsigned short、unsigned int、unsigned long、unsigned long long。

无符号(unsigned)由于没有负数,所以取值范围和带符号(signed)的也不同:

unsigned short :0~2^16 - 1

unsigned int :0~2^32 - 1

unsigned long :0~2^64 -1

注意:浮点数(float和double)没有无符号的。

2.unsigned类型转化

在上面知道了signed和unsigned的取值范围不同,并且unsigned不能为负数。

那么如果故意给unsigned一个负数,会发生什么呢,编译器会报错吗?答案是不会,编译器会认为你所做的含有特殊的含义,编译器不会干涉你。

那么负的unsigned转化为正的unsigned的规则是什么呢?

规则:结果值等于这个负数加上数据类型unsigned的模。

#include <iostream>
using namespace std;
int main(){
unsigned int n = -;
cout << "n = " << n << endl;
return ;
}

输出的结果值:

n = 4294967286

由于unsigned int的模为2^32(4294967296),2^32+(-10)就是4294967286。

unsigned short 的模为2^16。
unsigned int 的模为2^32。
unsigned long 的模为2^64。

这样以来的话,那么一个正的unsigned可以和一个负的unsigned(转化后为正的)相等。

unsigned int m = 4294967295;

unsigned int n = -1;

那么 m == n 结果为true。-1转化后恰好是4294967295。

3.字面值

整型和浮点数字面值

以0开头的整数表示八进制,以0x或0X开头的整数表示十六进制。

浮点型字面值默认是一个double类型。

转义序列

换行 \n 横向制表符 \t
纵向制表符 \v 退格符 \b
反斜线 \\ 问号 \?
回车符 \r 进纸符 \f
警报 \a 单引号 \'
双引号 \"    

指定字面值的类型

字符和字符串

前缀 含义 类型
u Unicode16字符 char16_t
U Unicode32字符 char32_t
L 宽字符 wchar_t
u8 UTF-8 char

整型字面值

后缀 类型
u or U unsigned
l or L long
ll or LL long long

浮点型字面值

后缀 类型
f or F float
l 或 L long double

指针字面值

nullptr是指针字面值

例如:

L'a' //wchar_t类型
42ULL //unsigned long long 类型
1E-3F //float类型
.14L //long double类型