strlen (think, "string length") is a C standard library function that returns the length of a string.
String length will not make much sense without a fundamental understanding of what strings in C are. Remember - strings are no more than arrays of ASCII-encoded characters ending with a terminating null byte (
\0). A pointer to a string is merely a pointer to the first character in this array.
For example, when we create a string
"Holberton", we truly create an array of ten ASCII characters
'\0'. In memory, this string can be represented like so:
char str1 = "Holberton";
Now, the length of this string is 9.
Did you do a double-take? No, I did not misspeak (Or, miswrite? What’s the proper term for articles?) - while the string
takes up ten bytes in memory, it’s length is not 10, but 9, corresponding directly to the number of non-null-byte characters.
Think about string length as word (or sentence) length, not size in memory.
const char *str
The function receives one parameter, a pointer to the string to measure the length of. Note that the string is received as a constant - the function
strlen will never alter the received string.
strlen returns a number, specifically, a
size_t (an unsigned integer type designed to hold any array index), representing the length of the received string.
strlen is declared as follows:
* strlen - Returns the length of a string.
* @str: The string to get the length of.
* Return: The length of @str.
size_t *strlen(const char *str)