strlen 的含义
strlen 是 C 语言中一个函数,用于求取一个字符串的长度。
使用方法
strlen 函数的语法如下:
立即学习“C语言免费学习笔记(深入)”;
<code class="C">size_t strlen(const char *str);</code>
其中:
str是要计算长度的字符串,类型为
char *。
size_t是一个无符号整数类型,表示字符串的长度。
返回值
strlen 函数返回字符串的长度,不包括字符串结束符
'\0'。
示例
以下代码演示了如何使用 strlen 函数:
<code class="C">#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, world!";
size_t length = strlen(str);
printf("The length of the string is: %d\n", length);
return 0;
}</code>输出
<code>The length of the string is: 13</code>
