数据类型
c++规定,定义变量或者常量的时候必需指定此变量或者常量的数据类型,意义在于根据数据类型分配适当大小的内存地址存储数据
整型
实型
注意:c++中的小数点前面的数字也算有效数字
float p = 3.12f;//默认小数是double 后面加个f就是float
double p1 = 3.141593;
默认情况下,小数显示的有效数字是六位
字符型
转义字符
字符串
int main() { //第一种:c风格的字符串 char str[] = "helloword"; //第二种:c++风格的字符串 string str = "helloc++"; }
布尔类型
bool flag = true; bool flag1 = true; cout << flag;//输入1 cout << flag1;//输入0 //本质上布尔类型的数据就是1为真0为假
sizeof
#include <iostream> using namespace std; int main() { int num = 10; int sum = 0; cout << sizeof(int);//可以使用sizeof统计数据类型或者变量所占大小 cout << sizeof(num); }