C String Length: Calculate Using strlen() Function

In the C language, you can use the built-in strlen() function to calculate the length of a string. This function is located in the string.h header file. Below is an example code:

#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;
}

In the example above, we defined a string array named str and initialized it with “Hello, World!”, then we used the strlen() function to calculate the length of the string and printed it out.

bannerAds