strlen 是 C 语言中的一个函数,用于计算字符串的长度。它接受一个字符串作为参数,并返回该字符
串的字符数(不包括结尾的空字符)。
例如,以下代码演示了如何使用 strlen 函数:
输出:
注意, strlen 函数计算的字符串长度不包括结尾的空字符。如果需要计算包括结尾空字符的字符串长
度,可以使用 sizeof 运算符。
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, world!";
int len = strlen(str);
printf("The length of the string is %d", len);
return 0;
}
复制代码
The length of the string is 13