C++语言的基础知识—(基本数据变量以及常用数据变量)
(原创)
基本数据类型
char,int,long,short,long long,float,double,long double
#include <iostream>
using namespace std;
int main()
{
int num1 = 2;
short num2 = 2;
long num3 = 2;
long long num4 = 2;
cout<<"int的占用内存为"<<sizeof(num1)<<endl;
cout<<"short的占用内存为"<<sizeof(num2)<<endl;
cout<<"long的占用内存为"<<sizeof(num3)<<endl;
cout<<"long long的占用内存为"<<sizeof(num4)<<endl;
float tital1 = 2.345366345f;
double tital2 = 2.345366345;
cout<<"tital1 = "<<tital1<<endl; //float默认保留6位有效数字
cout<<"tital2 = "<<tital2<<endl; //double默认保留六位有效数组
cout<<"float的占用内存为"<<sizeof(tital1)<<endl;
cout<<"double的占用内存为"<<sizeof(tital2)<<endl;
//科学计数法
float f1 = 3e2;//3*10^2
float f2 = 3e-2;//3*10^-2
cout<<"fl = "<<f1<<endl;
cout<<"f2 = "<<f2<<endl;
char ch = 'a';
cout<<"char的占用内存为"<<sizeof(char)<<endl;
cout<<"ch = "<<ch<<endl;
cout<<"ch修改为ASCII码后的值为"<<(int)ch<<endl;//转换为ASCII值
return 0;
}
常用数据类型
string,wchar_t,bool
#include<iostream>
#include <string>
using namespace std;
int main()
{
string sh = "Hello World!";
cout<<sh<<endl;
cout<<"sh的占用内存为"<<sizeof(sh)<<endl;
bool flag1 = true;
bool flag0 = false;
cout<<"flag1 = "<<flag1<<endl;
cout<<"flag0 = "<<flag0<<endl;
cout<<"bool的占用内存为"<<sizeof(bool)<<endl;
wchar_t cht = 'a';
cout<<"cht = "<<cht<<endl;
return 0;
}