查找宽字符串的长度

Finding length of wide character strings

要查找非宽字符串的长度,我们使用 strlen 函数:

#include <stdio.h>
#include <wchar.h>
#include <string.h>

int main(void)
{
char variable1[50] = "This is a test sent";
printf("The length of the string is: %d", strlen(variable1));
return 0;
}  
OUTPUT: 
The length of the string is: 18

我的书只说使用语法:

wcslen(const wchar_t* ws)

但是,我尝试了很多方法,但都没有用:

使用 D 格式说明符:

#include <stdio.h>
#include <wchar.h>
int main(void)
{
wchar_t variable1[50] = "This is a test sent";
printf("The length of the string is: %d", wcslen(const wchar_t* variable1));
return 0;
}  

使用 U 格式说明符

#include <stdio.h>
#include <wchar.h>

int main(void)
{
wchar_t variable[50] = "This is a test sent";
printf("The length of the string is: %u", wcslen(variable1));
return 0;
}  

使用 SIZE_T

类型的变量
#include <stdio.h>
#include <wchar.h>

int main(void)
{
wchar_t variable1[50] = "This is a test sent";
size_t variable2 = wcslen(char wchar_t* variable1);
printf("The length of the string is: %u", variable2);
return 0;
}  

我们如何找到字符串的长度,以便我们得到类似于第一个的输出?(最后三个代码都产生错误)

要启动宽字符串,ypu 必须在字符串前加上前缀 "L",例如:

wchar_t variable1[50] = L"This is a test sent";

这将使它成为一个 "wide" 字符串。