- 内置的C++类型分为两组:简单类型和复合类型,本章介绍基本类型:整数和浮点数。
3.1 简单变量
3.1.1 变量名
- 变量名命令规则:
1、在名称中只能使用字母字符、数字和下划线
2、名称的第一个字符不能是数字
3、区分大小写字符
4、不能将C++关键字用于变量名称
5、以两个下划线打头或下划线和大写字母打头的名称被保留给实现(编译器及其使用的资源)使用,一个下划线开头的名称被保留给实现,用作全局标识符
6、C++对于名称长度没有限制,但是平台会有限制
3.1.2 整型
- C++整型分别为:char、short、int、long、和C++11新增的long long。
3.1.3 整型short、int、long和long long
- C++中对类型的长度采取灵活的实现,确保了最小的长度:
1、short至少16位
2、int至少和short一样
3、long至少32位,且至少与int一样长
4、long long至少64位,且至少与long一样长
- Ubuntu 20.0.4 64位显示
#include <iostream>
#include <climits>
using namespace std;
int main() {
int a = INT_MAX;
short b = SHRT_MAX;
long c = LONG_MAX;
long long d = LLONG_MAX;
cout << "int size is " << sizeof a << endl;
cout << "short size is " << sizeof b << endl;
cout << "long size is " << sizeof c << endl;
cout << "long long size is " << sizeof d << endl;
cout << endl;
cout << "Maximum values:" << endl;
cout << "int: " << a << endl;
cout << "short: " << b << endl;
cout << "long: " << c << endl;
cout << "long long: " << d << endl;
return 0;
}
输出
int size is 4
short size is 2
long size is 8
long long size is 8
Maximum values:
int: 2147483647
short: 32767
long: 9223372036854775807
long long: 9223372036854775807
- 可对类型名或者变量名使用sizeof,当对类型名使用时,需要加括号,