Library Function strlen in String Class

Library Function strlen of string.h Header File

On this page we will discuss about library function strlen in string class which is used in C. The C header file string.h contains a set of functions that can be used for performing various manipulation operations on strings. The strlen function is used to determine the length of a given string.

Library Function strlen in String Class

Library Function strlen in String Class used in C

In C programming language the strlen function is included in string.h header file. The strlen function returns the number of characters in a string, but not including the null terminator.

Declaration of strlen function

 int strlen(const char *str);
The strlen function can also be declared as:
 size_t strlen(const char *str);
Here, strlen returns the length of the string as a size_t value.

Parameters of strlen function

The strcat function accepts single parameter which is str.

ParameterDescription
strIt is a pointer to a null-terminated string.

Return value of strlen function

The strlen function returns the length of the string as an integer value or of size_t value depending on the syntax used.

Implementation of Library Function string.h strlen

Example 1:

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

int main() {
    
    // Initializing string
    char str[] = "Hello, world!";
    
    //Using strlen() function
    int len = strlen(str);
    
    // Displaying the length of the string
    printf("The length of the string is %d\n", len);
    
    return 0;
}


Output:

The length of the string is 13.

Example 2:

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

int main()
{
    // Initializing string character by character
    char p[30]={'P','r','e','p','I','n','s','t','a','\0'};

    // Initializing strlen() function of size_t value.
    // using the %zu format specifier to print size_t
    printf("Length of the string p = %zu \n",strlen(p));

    return 0;
}

Output:

 Length of the string p = 9 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription