基本概念,基于asc ii至少使用一个字节表示字符,和unicode至少使用两个字节表示字符的事实,本次实验验证这个事实,同时揭示字符串字面量长度的计算......
main.cpp
#include <share/cpp/common.h>
int _tmain(_IN_ int argc,_IN_ TCHAR *argv[])
{
TCHAR *p_DemoText = _T("demoText");
char *p_demoText2 = "demoText";
_tcout << _T("the unicode \"demoText\" length: ") << _tcslen(p_DemoText) << _T(" the unicode \"demoText\" size: ") << sizeof(_T("demoText")) << endl;
cout << "the asc ii \"demoText\" length: " << strlen(p_demoText2) << " the asc ii \"demoText2\" size: " << sizeof("demoText2") << endl;
system("pause");
return (0);
}
share/cpp/common.h
#ifndef __COMMON_H__
#define __COMMON_H__
//whether unicode is used,to choose cout(asc ii version) or wcout(unicode version)
#ifndef _UNICODE
#define _tcout cout
#else
#define _tcout wcout
#endif
//Macros
#define _IN_
#define _OUT_
#include <iostream>
#include <tchar.h>
using namespace std;
#endif
可以看到,在计算字符串字面量的长度时,无论是asc ii还是unicode,函数strlen和_tcslen可以依据字符集进行正确的计算,在sizeof里面,asc ii和unicode就明显表现出了两者的区别....