写一个 C/C++ 程序,该程序打印基本类型的大小、一些指针类型和您选择的一些枚举。使用 sizeof 运算符
时间: 2024-03-26 19:35:25 浏览: 146
好的,以下是示例代码:
```c++
#include <iostream>
int main() {
// 打印基本类型的大小
std::cout << "Size of char: " << sizeof(char) << " bytes" << std::endl;
std::cout << "Size of short: " << sizeof(short) << " bytes" << std::endl;
std::cout << "Size of int: " << sizeof(int) << " bytes" << std::endl;
std::cout << "Size of long: " << sizeof(long) << " bytes" << std::endl;
std::cout << "Size of long long: " << sizeof(long long) << " bytes" << std::endl;
std::cout << "Size of float: " << sizeof(float) << " bytes" << std::endl;
std::cout << "Size of double: " << sizeof(double) << " bytes" << std::endl;
std::cout << "Size of long double: " << sizeof(long double) << " bytes" << std::endl;
// 打印指针类型的大小
std::cout << "Size of int*: " << sizeof(int*) << " bytes" << std::endl;
std::cout << "Size of char*: " << sizeof(char*) << " bytes" << std::endl;
std::cout << "Size of void*: " << sizeof(void*) << " bytes" << std::endl;
// 打印自定义枚举类型的大小
enum Color { RED, GREEN, BLUE };
std::cout << "Size of Color enum: " << sizeof(Color) << " bytes" << std::endl;
return 0;
}
```
输出结果:
```
Size of char: 1 bytes
Size of short: 2 bytes
Size of int: 4 bytes
Size of long: 8 bytes
Size of long long: 8 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of long double: 16 bytes
Size of int*: 8 bytes
Size of char*: 8 bytes
Size of void*: 8 bytes
Size of Color enum: 4 bytes
```
阅读全文