`strlen`函数用于计算一个字符串的长度,其基本用法如下:
1. 包含头文件:`include
2. 调用`strlen`函数,并将要计算长度的字符串作为参数传入。
3. 将返回值赋给一个变量,即可得到字符串的长度。
`strlen`函数返回的是一个无符号整型数值,表示字符串中字符的个数,直到遇到字符串结束符`'\0'`为止,不包括结束符`'\0'`。
下面是一个简单的C语言示例,展示如何使用`strlen`函数:
```c
include
include
int main() {
char str[] = "Hello, World!";
size_t len = strlen(str);
printf("The length of the string is: %zu\n", len);
return 0;
}
输出结果为:
```
The length of the string is: 13
请注意,如果传入的参数不是一个以`'\0'`结尾的字符串,`strlen`函数的行为是未定义的,可能会导致程序错误