您的位置:首页 > 其它

strlen,wcslen,lstrlen函数与sizeof运算符

2011-03-14 14:25 525 查看
#ifdef UNICODE

#define lstrlen lstrlenW

#else

#define lstrlen lstrlenA

#endif

所以在Unicode下,lstrlen等同lstrlenW(LPCWSTR lpString),在非Unicode下等同lstrlenA(LPCSTR lpString)。而lstrlenW又等同于wcslen,lstrlenA又等同于strlen,只不过一个是C的标准函数,一个是WinAPI函数。

所以只需讨论strlen,wcslen与sizeof的区别。

#include "stdafx.h"

#include "windows.h"

#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{

char str1[]="abcde";

char str2[]="我是中国人";

WCHAR str3[]=L"abcde";

WCHAR str4[]=L"我是中国人";

cout<<strlen(str1)<<endl;

cout<<sizeof(str1)<<endl;

cout<<endl;

cout<<strlen(str2)<<endl;

cout<<sizeof(str2)<<endl;

cout<<endl;

cout<<wcslen(str3)<<endl;

cout<<sizeof(str3)<<endl;

cout<<endl;

cout<<wcslen(str4)<<endl;

cout<<sizeof(str4)<<endl;

cout<<endl;

return 0;

}

输出结果:

5

6

10

11

5

12

5

12

请按任意键继续. . .

由此可见,strlen返回的是字节数(对中英文不一致,中文占两个字节,不包括'/0'),而wcslen返回的是字符数(对中英文一致)。而sizeof返回的是字节数(包含'/0',而'/0'在Unicode下也是占两个字节的)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: