c++ 中字符串的字符数与字节数

c++中字符数是字符的个数,字节数是在内存中所占的大小(siziof()),每一个字符串在内存中都以'/0'结束。

#include
using namespace std;
#include 
int main()
{
	string str_string="test13";
	CString str_Cstring=_T("test11");
	char str_char='t';
	char strchar[]="qwer";

    //二维字符数组测试
	char a[3][4]={"111","132","323"};

	a[2][2]=a[2][1];
	a[2][2]='1';
	char fore[4]="111";
	//scanf("%s",a[2]); //可赋值
	strcpy(a[2],"111");  //字符串拷贝
	memcpy(a[2],"222",4); //内存拷贝
	//二维字符数组测试
	
	printf_s(_T("str_string字符数为:%d\n"),str_string.size());
	printf_s(_T("str_Cstring字符数为:%d\n"),str_Cstring.GetLength());

	printf_s(_T("str_string字节数为:%d\n"),sizeof(str_string));    //string是类,不是数据类型,只是处理字符串的类,可以转换为字符串char a[大小];  strcpy(a,str_string.c_str);(a字符串以'\0结尾')
	printf_s(_T("str_Cstring字节数为:%d\n"),sizeof(str_Cstring));   //类似于char[]数组,存放字符串都以'\0'结尾,'\0'占一个字节
	printf_s(_T("str_char字节数为:%d\n"),sizeof(str_char));
	printf_s(_T("strchar字节数为:%d\n"),sizeof(strchar));

	printf_s(_T("本机基本变量类型所占字节数\n"));
	printf_s(_T("char所占字节数:%d\n"),sizeof(char));
	printf_s(_T("BYTE所占字节数:%d\n"),sizeof(BYTE));     //typedef unsigned char       BYTE;
	printf_s(_T("int所占字节数:%d\n"),sizeof(int));
	printf_s(_T("float所占字节数:%d\n"),sizeof(float));
	printf_s(_T("short所占字节数:%d\n"),sizeof(short));
	printf_s(_T("WORD所占字节数:%d\n"),sizeof(WORD));     //typedef unsigned short      WORD;
	printf_s(_T("float所占字节数:%d\n"),sizeof(float));
	printf_s(_T("double所占字节数:%d\n"),sizeof(double));
	printf_s(_T("longdouble所占字节数:%d\n"),sizeof(long double));
	printf_s(_T("long所占字节数:%d\n"),sizeof(long));   
	printf_s(_T("long long所占字节数:%d\n"),sizeof(long long)); 
	printf_s(_T("DWORD所占字节数:%d\n"),sizeof(DWORD));  //typedef unsigned long       DWORD;
	printf_s(_T("bool所占字节数:%d\n"),sizeof(bool));    //标准c++ bool类型
	printf_s(_T("BOOL所占字节数:%d\n"),sizeof(BOOL));    //typedef int                 BOOL;
	printf_s(_T("uint所占字节数:%d\n"),sizeof(UINT));    //typedef unsigned int        UINT;


	system("pause");
}

测试结果如下:

c++ 中字符串的字符数与字节数_第1张图片

你可能感兴趣的:(windows,c/c++,c++)