strlen strlen函数

C语言的strlen函数怎么使用

小亿
147
2023-06-25 11:39:26
栏目: 编程语言
C语言开发专用服务器,限时0元免费领! 查看>>

strlen函数用于计算字符串的长度,其语法如下:

size_t strlen(const char *s);

其中,参数s是一个指向以null结尾的字符串的指针。函数返回值是字符串s的长度,不包括null字符。

使用示例:

#include <stdio.h>
#include <string.h>
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 11

注意,字符串必须以null字符结尾,否则strlen函数会继续计算字符串长度,直到遇到null字符为止。如果字符串没有null字符,那么strlen函数的行为是未定义的。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

相关推荐:c语言strlen函数的使用方法是什么

0
看了该问题的人还看了