How to use the strlen() function in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

What is String length in C?

The string length in C is equal to the count of all the characters in it (except the null character "\0"). String length in C For example, the string "gfddf" has a length of 5 and the string "4343" has a length of 4.

Note: In C, a String is an array of characters that is terminated with a null character "\0" to indicate the end of the string.

How to Find the String length in C?

We can find the length of a String by counting all the characters in it (before the null character) using a for loop. We can also use the built-in string.h library function strlen() or the sizeof() operator.
Let us understand how to use these methods in the coming sections.

Syntax of the string length in C

The following is the syntax of the strlen() function:

The strlen() function takes a string as an argument and returns its length. The returned value is of type size_t (an unsigned integer type).

Implementation of string length in C

The following example implements the strlen() function in C:

Understanding the code:

In the code above, lines 10 and 11 are calculating the length of the two given strings (string1 and string2). Both strings were initialized at the beginning of the code on lines 6 and 7.

You might have noticed, in the output displayed on the console, that the length of string2 is 8 instead of 9. This is because strlen() does not count the null character (\0) while counting.

Example 1: Using loop to calculate the length of string

We can find the length of a string using a user-defined function by iterating over the string in a for loop and incrementing the count of characters (length) by 1 till it reaches the null character "\0" and then returning the count.

Let's take a look at its implementation:
Code

Output

Explanation

In the above example, we are finding the length of a string by iterating over it in a for loop and incrementing the count by 1 in each iteration till it reaches the null character. The value of count after the end of the for loop will be equal to the length of the string.

Example 2: Using strlen() to find the length of the string

The length of a string can be found using the built-in library function strlen() in string.h header file. It returns the length of the string passed to it (ignoring the null character).
String Length in C using Built-In Library

Syntax

The string is passed to the strlen() function, which returns its length.

Let us take a look at its implementation.
Code

Output :

In the above example, we are finding the length of a string by using the string.h library function strlen(), which returns the length of the string passed to it.

Example 3: Using sizeof() to find the length of the string

We can also find the length of a string using the sizeof Operator in C. The sizeof is a unary operator which returns the size of an entity (variable or value).
String Length in C using the sizeof Operator Syntax

The value is written with the sizeof operator which returns its size (in bytes).

Let us take a look at its implementation:
Code

Output

In the above example, we are finding the length of the string "g4343" using the sizeof operator in C. The sizeof operator returns its length as 6 in the output.

Example 4: Using Pointer Subtraction

Code

Output:

Explanation:

  • In the main() function, a character array str[] is defined and initialized with the string “Hello“.
  • A pointer ptr is declared and initialized with the starting address of the str = e996300 array.
  • The code enters a while loop that iterates until the value pointed to by ptr is not null. In C, the null character ‘\0‘ signifies the end of a string.
  • Inside the loop, the ptr is incremented to point to the next character in the string.
  • After the loop, the code calculates the length of the string by subtracting the starting address of str = e996300 from the final value of ptr = e996305. This pointer subtraction gives the number of elements (characters) between the two addresses

Conclusion

  • In C, the length of a string is the count of all its characters except the null character "\0".
  • We can find the length of the string using a for loop by counting each character in each iteration.
  • The built-in library function strlen() is used to return the length of a string passed to it.
  • We can also find the length of a C String using the sizeof operator.