How do you calculate the length of a string in the C language?

In C language, you can use the strlen() function to calculate the length of a string.

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

int main() {
    char str[] = "Hello, world!";
    int len = strlen(str);
    printf("字符串的长度为:%d\n", len);
    return 0;
}

The output is:

字符串的长度为:13

It is important to note that the strlen() function calculates the actual length of a string, excluding the null character \0 at the end.

bannerAds