整形、实型、字符型、关键词
数据类型—整形
数据类型—sizeof关键词
数据类型—实型
数据类型—字符型
#include <iostream>
using namespace std;
int main()
{
short b = 10;
cout << "a=" << b << endl;
long int c = 10;
cout << "a=" << c << endl;
long long int d = 10;
cout << "a=" << d << endl;
int a = 10;
cout << "a=" << a<<endl;
return 0;
}
今天学了不同数据类型,所占内存大小不一样;尽量写数据类型差不多的;
short 短整型 2 范围为-2的15次方~2的15次方-1
int 整形 4 -2的31次方~2的31次方-1
long 长整形 4 -2的31次方~2的31次方-1
long long 长长整形 8 -2的63次方~2的63次方-1
因此对于上面求a的值 就把a的类型定为int;C++中整形定为int;
7/6号 sizeof(数据类型/变量) 求数据类型所占内存大小
#include <iostream>
using namespace std;
#include <stdio.h>
int main()
{
int num1 = 10;
cout << "num1=" << sizeof(int) << endl;
short num2 = 10;
cout << "num1=" << sizeof(short) << endl;
long num3 = 10;
cout << "num1=" << sizeof(num3) << endl;
long long num4 = 10;
cout << "num1=" << sizeof(num4) << endl;
system("pause");
return 0;
}
总结:sizeof(数据类型/变量名)
7/6 本节课学习了数据类型--实型
#include <iostream>
using namespace std;
int main()
{
单精度 float 4个字节 有七位有效数字
双精度 double 8个字节 有15-16位效数字
两者的区别为双精度比单精度更精准;
科学计数法
float c = 3e2; //表示3*10^2;
cout << "c=" << c << endl;
double d = 3e2;
cout << "d=" << d << endl;
float e = 3e-2;
cout << "e=" << e << endl;
double f = 3e-2; //3*0.1^2;
cout << "f=" << f << endl;
float a = 3.1415926f; //注:要是使用float类型,需要在小数后面加上f;
cout << "a=" << a << endl;
double b = 3.1415926;
cout << "b=" << b<< endl;
system("pause");
return 0;
}
7/6 本节课学习了char 类型
#include <iostream>
using namespace std;
int main()
{
//关于 char 要明白四个问题
//char所占内存大小
char ch = 'a';
cout << "ch=" << ch << endl;
cout << "ch所占的内存大小为:" << sizeof(ch) << endl;
//char的使用 char ch = 'b';
//char常见的错误 char ch = 'b'; // 使用char是必须是单引号‘’;单引号‘’里面必须是一个字母;
//char所对应的ASCLL值 char ch = 'a';
cout << "ch=" << (int)ch << endl; //求字母所对应的ASCLL值时需要,强制类型转换,就是在变量前加 int ;
system("pause");
return 0;
}
以上内容比较乱,哈哈哈,别在意😂