温馨提示×

c语言怎么计算字符串长度

小亿
121
2024-03-01 13:25:11
栏目: 编程语言

在C语言中,可以使用内置的strlen()函数来计算字符串的长度。该函数位于string.h头文件中。以下是一个示例代码:

#include <stdio.h> #include <string.h> int main() { char str[50] = "Hello, World!"; int length = strlen(str); printf("Length of the string: %d\n", length); return 0; } 

在上面的示例中,我们定义了一个字符串数组str,并初始化为"Hello, World!",然后使用strlen()函数来计算字符串的长度,并打印出来。

0