c++标准保证:1 == sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
void类型:值为空值的类型。也不存在含有void的数组以及到void的引用;但是指向void的指针和返回void类型的函数是允许存在的。
如以下代码:
#include<iostream>
using namespace std;
void MyPrintf(void);
int main(int argc, const char* argv[])
{
void array[10]; //错误,不允许使用void的数组
void* ptr; //正确
MyPrintf(); //正确
return 0;
}
void MyPrintf(void)
{
cout << "hello world" << endl;
}
bool类型:足以存放两个值true和false之一的类型,sizeof(bool)的值由实现定义,而且不一定是1。
如以下代码:
#include<iostream>
using namespace std;
int main(int argc, const char* argv[])
{
bool number;
number = true;
cout << number << endl; //true为真,此时number的值也应为1
number = false;
cout << number << endl; //false为假,此时number的值也应为0
cout << "sizeof(bool):" << sizeof(bool) << endl;
return 0;
}
数据模型:我们重点关注64位系统下的数据模型。int和long是32位,指针是64位。
如以下代码:
#include<iostream>
using namespace std;
int main(int argc, const char* argv[])
{
int* ptr;
cout << "sizeof(int):" << sizeof(int) << endl; //4字节
cout << "sizeof(long):" << sizeof(long) << endl; //4字节
cout << "sizeof(ptr):" << sizeof(ptr) << endl; //8字节
return 0;
}
整数类型:short、int、long。
修饰符:signed(有符号)、unsigned(无符号)。
long long 目标类型至少有64位的宽度。
如以下代码:
#include<iostream>
using namespace std;
int main(int argc, const char* argv[])
{
signed int number1 = -100; //有符号数
unsigned int number2 = 100; //无符号数
cout << number1 << endl;
return 0;
}
字符类型:char。
浮点类型:float、double。
如以下代码:
#include<iostream>
using namespace std;
int main(int argc, const char* argv[])
{
float number1 = 1.1; //单精度浮点数
double number2 = 2.2; //双精度浮点数
return 0;
}