计算中文在内存占用的字节数

#include<iostream>
using namespace std;


void main()
{
	// 功能: 计算 中文在内存占用的字节数

	char name[] = "成都";
	char node_name[] = "成都移动04节点";
	cout << "int占用:" << sizeof(int) << endl;
	cout << "成都:" << sizeof(name) << endl;
	cout << "成都:" << strlen(name) << endl;
	cout << "成都移动04节点占用:" << sizeof(node_name) << endl;
	cout << "成都移动04节点占用:" << strlen(node_name) << endl;
}

注:

  1.  sizeof() 函数不统计字符串结束标识'\0'。
  2. strlen() 函数统计时包含字符串结束标识'\0'。

结果:

中文具体在内存中占用多少字节取决于你的电脑是64位还是32位。你可以执行上面的代码查看一下,当前大部分电脑都是64位的。

posted on 2022-10-26 21:17  进取  阅读(46)  评论(0编辑  收藏  举报