Javatpoint Logo
Javatpoint Logo

Strlen() function in C

Strings are an essential component of many C programming applications. Understanding the various functions linked to strings is essential for efficient string manipulation and processing. The strlen() function is used to find the length of a string. This blog post will go into detail about the strlen() function, including its syntax, implementation, examples, and expected results.

Syntax of Strlen():

The string.h library in C has the strlen() method, which has the following syntax:

Here is a description of each element:

Include the string.h file: The header file required to use the strlen() function is included on this line.

size_t is a type that indicates an object's size in bytes.

strlen: The function's name.

const char *str: The function's str parameter is a pointer to a character that is a constant and stands in for the string whose length needs to be calculated.

By counting the characters up until the null-terminating character ('0'), the strlen() method determines the length of a given string. It gives back the length as a size_t value.


Let's look at an illustration of how to use the strlen() function:


Length of the string: 13


In the example above, the string "Hello, World!" is contained in a character array called greeting. After that, the length of the string is determined and stored in the variable length using the strlen() function. The length of the string is displayed as 13 when we use printf() to print the value of length.

Here is another illustration of how to use the strlen() function:


Length of the sentence: 19


The example above includes the string "I love programming!" in a character array called a sentence. The length of the string is determined using the strlen() method, which is then saved in the variable length. Finally, we use printf() to output the length value, which shows that the sentence is 19 words long.

In this situation, the strlen() method counts every character in the string until it encounters the null-terminating character ('0'). Therefore, the sentence is 19 words long when all the alphabets, spaces, and exclamation points are considered.


The null-terminating character ('0') is excluded from the length calculation by the strlen() function. Only the characters up until the null character are counted. Make sure the input string is null-terminated if possible. Otherwise, strlen() will behave in an unpredictable manner.

strlen() will return 0 if an empty string is supplied to it (a string with only the null character at the end).

An unsigned integer type with the purpose of representing sizes, the size_t type, is used as the return type of strlen(). Therefore, negative lengths are not conceivable.

Common Pitfalls:

I forgot to include the header <string.h>: The string.h header must be present to use the strlen() method.

Using a string that has been wrongly initialized or terminated: Undefined behavior could result if the string supplied to strlen() is not null-terminated.

In printf(), the format specifier is incorrect: Use the appropriate format specifier %zu to output size_t values when printing the result.


In conclusion, the C language's strlen() function offers a practical and effective approach to calculating the length of a string. It determines the string's exact length by counting characters up until the null-terminating character ('0'). To get correct results, it is crucial to comprehend the syntax and appropriate application of strlen().

The strlen() function's syntax, implementation, and examples with appropriate outputs were all covered in this blog post. Additionally, we called attention to crucial details like the necessity of null-terminated strings and strlen()'s behavior with empty strings.

C programmers can execute a variety of actions on strings and base choices on the length of strings by utilizing the strlen() function's capability. It is a useful tool for creating reliable and effective string manipulation apps.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA