目录
3_9整型变量的定义
#include <iostream>
using namespace std;
int main()
{
int a=-1;
unsigned b=-2;
short c=-3;
long d=-4;
unsigned long e;
e=a+b+c+d;
cout<<"a:"<<a<<"\n"<<"b:"<<b<<"\n"
<<"c:"<<c<<"\n"<<"d:"<<d<<"\n"
<<"e:"<<e;
return 0;
}
//数据溢出是不会报错的,而是像汽车里程表那样,到达最大值后,又从最小值开始计数。
//因此需要时刻注意所定义的最大值取值范围,一定不要超过这个范围进行赋值。
3_10浮点型变量
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double a=12.12345678998754621;
cout<<setprecision(21) <<a;//头文件中的setprecision用来设置输出的精度 比如设置5 则输出12.12
return 0;
}
//float型变量的精度为6~7位,而double的有效位数为15~16
3_11常量
#include<iostream>
using namespace std;
int main()
const int a=1;//将a定义为int型常量
{
a=2;//这里试图对a赋值,则无法通过编译
return 0;
}
3_12枚举型常量
#include <iostream>
using namespace std;
int main()
{
enum day{A,B,C,D,E,F,G};
day LL;
LL=C;
if (LL==A||LL==B)
{
cout<<"!\n"<<LL;
}
else
{
cout<<"?\n"<<LL;
}
return 0;
}
//用关键字enum将day声明为枚举类
//用枚举类day创建了LL变量
//枚举类的第一个常量默认为0,其他一次增加,也可以自定义。
//枚举类的作业,用文字代替数字,这样做可以是程序变得更容易读懂