#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 版權聲明:本文爲博主原創文章,轉載請附上博文連接!