C语言的strlen函数如何使用
C语言的strlen函数如何使用
strlen函数用于计算字符串的长度,其语法以下:
```c
size_t strlen(const char *s);
```
其中,参数s是一个指向以null结尾的字符串的指针。函数返回值是字符串s的长度,不包括null字符。
使用示例:
```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 11
```
注意,字符串一定要以null字符结尾,否则strlen函数会继续计算字符串长度,直到遇到null字符为止。如果字符串没有null字符,那末strlen函数的行动是未定义的。
文章来源:丸子建站
文章标题:C语言的strlen函数如何使用
https://www.wanzijz.com/view/73012.html