输出整形int、short、long、float、double的长度,int类型变量的长度,常量5和5.2的长度c++代码
时间: 2024-06-05 19:07:26 浏览: 84
#include <iostream>
using namespace std;
int main() {
cout << "Size of int: " << sizeof(int) << " bytes" << endl;
cout << "Size of short: " << sizeof(short) << " bytes" << endl;
cout << "Size of long: " << sizeof(long) << " bytes" << endl;
cout << "Size of float: " << sizeof(float) << " bytes" << endl;
cout << "Size of double: " << sizeof(double) << " bytes" << endl;
int num = 5;
int sizeOfNum = sizeof(num);
cout << "Size of num: " << sizeOfNum << " bytes" << endl;
const int num2 = 5;
int sizeOfNum2 = sizeof(num2);
cout << "Size of num2 (constant int): " << sizeOfNum2 << " bytes" << endl;
const float num3 = 5.2f;
int sizeOfNum3 = sizeof(num3);
cout << "Size of num3 (constant float): " << sizeOfNum3 << " bytes" << endl;
return 0;
}
输出结果为:
Size of int: 4 bytes
Size of short: 2 bytes
Size of long: 8 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of num: 4 bytes
Size of num2 (constant int): 4 bytes
Size of num3 (constant float): 4 bytes
阅读全文