C++—— C++数据类型
1、数据类型分类
(1)基本数据类型
字符型(char)—— 1字节
整型(int) —— 4字节
浮点型(实型)—— 单精度float4字节;双精度(double) 8字节
逻辑性(bool)
(2)引申类型
枚举类型(enum)
数组类型([])
指针类型(*)
引用类型(&)
(3)构造类型
结构体类型(struct)
共用体类型(union)
类类型(class)
(4)空类型void
不同的C++编译系统对数据占内存的字节数可能会有所不同
数据类型
字节数
取值范围
char
1
-128~127
unsigned char
1
0~255
bool
1
true(1);false(0)
short
2
-32768~32767
unsigned short
2
0~65536
int
4
-2147483648~2147483647
unsigned int
4
0~4294967295
long
4
-2147483648~2147483647
unsigned long
4
0~4294967295
float
4
-3.4×1038~3.4×1038,6/7位有效数字
double
8
15/16位有效数字
long double
8
15/16位有效数字
/************************************************************************* > File Name: test.cpp > Author: libang > Mail: 18186747463@163.com > Created Time: 2016年09月27日星期二 20时44分32秒 ************************************************************************/ // (使用运算符sizeof计算变量占内存的字节数) #include <iostream> using namespace std; int main(void) { cout << "sizeof(char): "<< sizeof(char) << endl; cout << "sizeof(bool): "<< sizeof(bool) << endl; cout << "sizeof(short): "<< sizeof(short) << endl; cout << "sizeof(int): "<< sizeof(int) << endl; cout << "sizeof(long): "<< sizeof(long) << endl; cout << "sizeof(float): "<< sizeof(float) << endl; cout << "sizeof(double):" << sizeof(double) << endl; return 0; }g++ test.cpp./a.out
2、字符集【构成C++语言的基本元素】
52个大小写的英文字母:A~Z,a~z
10个数字字符:0~9
特殊字符:空格 ! # % ^ & * _(下划线) + = | - ~ < > / \ ' " : ; . , ( ) [] { } ?
3、标识符
【用字符序列来标识变量、常量、函数等】
标识符是以字母或下划线开头,后跟若干个字母、下划线或者数字的有效字符序列。标识符有大小写之分。