Function to 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.

The prototype of the strlen function is: size_t strlen(const char *str)

str is a pointer to a string that ends with a null character (‘\0’).

The example code is as follows:

#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 output shows: The string’s length is 13.

bannerAds