C++ strlen()

The strlen() function in C++ returns the length of the given C-string. It is defined in the cstring header file.

Example


strlen() Syntax

The syntax of the strlen() function is:

strlen(const char* str);

Here, str is the string whose length we need to find out, which is casted to a const char*.


strlen() Parameters

The strlen() function takes the following parameter:

  • str - pointer to the C-string (null-terminated string) whose length is to be calculated

strlen() Return Value

The strlen() function returns:

  • the length of the C-string (size_t)

strlen() Prototype

The prototype of strlen() as defined in the cstring header file is:

size_t strlen(const char* str);

Note: The returned length does not include the null character '\0'.


strlen() Undefined Behavior

The behavior of strlen() is undefined if:

  • there is no null character '\0' in the string i.e. if it is not a C-string

Example: C++ strlen()

Output

Length of str1 = 13
Length of str2 = 22
str2 is longer than str1