sizeof() 运算符
sizeof() 运算符 用来计算所占每各种整型空间的大小;可以简单的了解一下,如果大程序中出现空间的报错,可以在定义不同的整型来减少内存的占用;
使用方式见下列代码:
#include <iostream>
using namespace std;
int main()
{
char c;
cout << "char型的大小 :" << sizeof(char) << '\n';
cout << "变量c的大小 :" << sizeof(c) << '\n';
short h;
cout << "short型的大小 :" << sizeof(short) << '\n';
cout << "变量h的大小 :" << sizeof(h) << '\n';
int i;
cout << "int型的大小 :" << sizeof(int) << '\n';
cout << "变量i的大小 :" << sizeof(i) << '\n';
long l;
cout << "long型的大小 :" << sizeof(long) << '\n';
cout << "变量l的大小 :" << sizeof(l) << '\n';
return 0;
}
运行结果为:

将char的size作为一个存储单位,那么剩下的变量的存储的大小可以表示为:

typedef()运算符
**typedef()😗*给变量一个别称,使用方法如下:
typedef unsigned size_t; // 定义示例: size_t 是 unsigned 的同义词
typeid()运算符
typeid():得到变量的类型,使用方法如下:
#include <iostream>
#include <typeinfo>
using namespace std;
int main()
{
char c;
short h;
int i;
long l;
cout << "变量c的类型:" << typeid(c).name() << '\n';
cout << "变量h的类型:" << typeid(h).name() << '\n';
cout << "变量i的类型:" << typeid(i).name() << '\n';
cout << "变量l的类型:" << typeid(l).name() << '\n';
cout << "字符字面量'A'的类型:" << typeid('A').name() << '\n';
cout << "整数字面量100的类型:" << typeid(100).name() << '\n';
cout << "整数字面量100U的类型:" << typeid(100U).name() << '\n';
cout << "整数字面量100L的类型:" << typeid(100L).name() << '\n';
cout << "整数字面量100UL的类型:" << typeid(100UL).name() << '\n';
return 0;
}


被折叠的 条评论
为什么被折叠?



