How to obtain the length of a char array in C language?

In C language, we can use the strlen() function to get the length of a char array. This function is located in the string.h header file, takes a char array as a parameter, and returns the length of the array (excluding the ending null character ‘\0’).

The sample code is shown below:

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

int main() {
    char str[] = "Hello World";
    int length = strlen(str);
    
    printf("Length of the string: %d\n", length);
    
    return 0;
}

In the example above, the strlen() function is used to get the length of the str array and store the result in the variable length. Finally, the printf() function is used to print out the length of the array.

 

More tutorials

How can we use strlen in C language to calculate the length of an array?(Opens in a new browser tab)

convert string to character array in Java.(Opens in a new browser tab)

Converting string to array in the Java programming language(Opens in a new browser tab)

Convert time to hours, minutes, and seconds in python(Opens in a new browser tab)

How can Oracle extract a specific substring?(Opens in a new browser tab)

 

Leave a Reply 0

Your email address will not be published. Required fields are marked *