C strlen

This string function is used to find a length of a string. It counts the number of characters present in a string. But it does not count ‘\0’

Syntax for strlen() –

Example:

Output:

Note that while calculating the length it doesn’t count ‘\0’. In the string2 it also counts space between two words, hello and world. Because compiler count length till pointer doesn’t point to ‘\0’. The following figure will explain memory allocation of string2.

strlen example

From figure, strlen(&str2[2]) it calculate length from given address i.e, from 102 upto ‘\0’.
strlen(str2 + 5) in this, an address of str2 is 100 added to the integer value 5, therefore, we get 105. Now from 105 to ‘\0’ length will calculate.
In string3 compiler stop calculating length when the pointer points to ‘\0’ even string is not completed.

Ankit Wasankar

Senior Java Developer
Java 11 | Microservices | Reactjs | SQL

Help others by sharing the content!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.