C String Length: strlen() function

The strlen() function is a commonly used function in the C programming language that allows you to determine the length of a null-terminated string. It is declared in the <string.h> header file.

Here is the prototype of the strlen() function:

size_t strlen(const char *str);

The function takes a pointer to a null-terminated string as its argument and returns the number of characters in the string, excluding the null terminator.

Here’s an example usage of strlen():

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

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

In this example, the strlen() function is used to determine the length of the string stored in the str variable. The result is then printed to the console.

Note that the strlen() function only works correctly with null-terminated strings. If you pass a string that is not properly null-terminated, the behavior will be undefined, and the function may produce incorrect results or crash.