#include <iostream>
int main() {
// sizeof 操作符用于计算数据类型或变量的大小(以字节为单位)
// 基本数据类型的大小
std::cout << "Size of char: " << sizeof(char) << " bytes" << std::endl;
std::cout << "Size of int: " << sizeof(int) << " bytes" << std::endl;
std::cout << "Size of float: " << sizeof(float) << " bytes" << std::endl;
std::cout << "Size of double: " << sizeof(double) << " bytes" << std::endl;
// 自定义类型的大小
struct MyStruct {
int a;
double b;
};
std::cout << "Size of MyStruct: " << sizeof(MyStruct) << " bytes" << std::endl;
// 数组的大小
int arr[5];
std::cout << "Size of arr: " << sizeof(arr) << " bytes" << std::endl;
// 指针的大小
int* ptr = &arr[0];
std::cout << "Size of ptr: " << sizeof(ptr) << " bytes" << std::endl;
return 0;
}
sizeof
是 C++ 中的一个操作符,用于计算数据类型或变量的大小(以字节为单位)。char
占 1 字节,int
占 4 字节,float
占 4 字节,double
占 8 字节,指针在 32 位系统上占 4 字节,在 64 位系统上占 8 字节。上一篇:c++深度学习
下一篇:c++运算优先级
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站