温馨提示×

c语言中计算字符串长度的函数

小亿
142
2024-01-08 13:33:46
栏目: 编程语言
C语言开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在C语言中,可以使用strlen函数来计算字符串的长度。

strlen函数的原型为:size_t strlen(const char *str)

其中,str是一个指向以null (‘\0’) 字符结尾的字符串的指针。

示例代码如下所示:

#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "Hello, world!";
    int length = strlen(str);
    printf("The length of the string is: %d\n", length);
    return 0;
}

输出结果为:The length of the string is: 13

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

推荐阅读:c语言中怎么计算字符串长度

0