sizeof.cpp

来自「c语言教程源码」· C++ 代码 · 共 38 行

CPP
38
字号
//这个程序在本书所带软盘中,文件名为SIZEOF.CPP
//这个程序利用sizeof和sizeof()来显示各种数据类型所占内存字节数。

#include <iostream.h>

void main(void)
{
	char ch = 'a';
	int num = 32767;
	short snum = 100;
	long lnum = 99999;
	float pi = 3.141596;
	double real = 3e10;

	int char_bytes, int_bytes, short_bytes, long_bytes, float_bytes, double_bytes;

	cout << "  字符型数据所占字节数: " << sizeof(char) << endl;
	cout << "  整数型数据所占字节数: " << sizeof(int) << endl;
	cout << "  短整型数据所占字节数: " << sizeof(short) << endl;
	cout << "  长整型数据所占字节数: " << sizeof(long) << endl;
	cout << "  浮点型数据所占字节数: " << sizeof(float) << endl;
	cout << "双精度型数据所占字节数: " << sizeof(double) << endl;

	char_bytes = sizeof ch;
	int_bytes = sizeof num;
	short_bytes = sizeof snum;
	long_bytes = sizeof lnum;
	float_bytes = sizeof pi;
	double_bytes = sizeof real;

	cout << endl;
	cout << "    字符型变量ch所占内存字节数: " << char_bytes << endl;
	cout << "     整型变量num所占内存字节数: " << int_bytes << endl;
	cout << "  短整型变量snum所占内存字节数: " << short_bytes << endl;
	cout << "  长整型变量lsum所占内存字节数: " << long_bytes << endl;
	cout << "    浮点型变量pi所占内存字节数: " << float_bytes << endl;
	cout << "双精度型变量real所占内存字节数: " << double_bytes << endl;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?