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

#ifdef UNICODEios

#define lstrlen  lstrlenW
#else
#define lstrlen  lstrlenA
#endifwindows

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

因此只需讨论strlen,wcslen与sizeof的区别。spa

 

#include "stdafx.h"
#include "windows.h"
#include <iostream>
using namespace std;.net

int _tmain(int argc, _TCHAR* argv[])
{
 char str1[]="abcde";
 char str2[]="我是中国人";
 WCHAR str3[]=L"abcde";
 WCHAR str4[]=L"我是中国人";code

 cout<<strlen(str1)<<endl;
 cout<<sizeof(str1)<<endl;
 cout<<endl;blog

 cout<<strlen(str2)<<endl;
 cout<<sizeof(str2)<<endl;
 cout<<endl;io

 cout<<wcslen(str3)<<endl;
 cout<<sizeof(str3)<<endl;
 cout<<endl;stream

 cout<<wcslen(str4)<<endl;
 cout<<sizeof(str4)<<endl;
 cout<<endl; gc

 return 0;
}

输出结果:

5
6

 

10
11

 

5
12

 

5
12

请按任意键继续. . .

因而可知,strlen返回的是字节数(对中英文不一致,中文占两个字节,不包括'/0'),而wcslen返回的是字符数(对中英文一致)。而sizeof返回的是字节数(包含'/0',而'/0'在Unicode下也是占两个字节的)。 ---------------------  做者:hczhiyue  来源:CSDN  原文:https://blog.csdn.net/hczhiyue/article/details/6248229  版权声明:本文为博主原创文章,转载请附上博文连接!

相关文章
相关标签/搜索