The strlen function is used in C language to calculate the length of a given string (excluding the terminating null character) and return the number of characters in the string. It accepts a string pointer as a parameter and returns an unsigned integer value representing the length of the string.
The role of strlen function in C language
The strlen function is a C language standard library function used for calculations The length of the given string. It returns the number of characters in the string (excluding the terminating null character '\0').
Usage
The syntax of strlen function is as follows:
<code class="c">size_t strlen(const char *str);</code>
where,
Return value
The strlen function return type is size_t, which represents an unsigned integer type, which contains the length of the string.
Example
The following example demonstrates the use of the strlen function:
<code class="c">#include <stdio.h> #include <string.h> int main() { char str[] = "Hello, world!"; int len; // 计算字符串的长度 len = strlen(str); // 输出字符串的长度 printf("字符串 '%s' 的长度为:%d\n", str, len); return 0; }</code>
Output:
<code>字符串 'Hello, world!' 的长度为:13</code>
Note:
<string.h>
needs to be included before using it. The above is the detailed content of The role of strlen function in c language. For more information, please follow other related articles on the PHP Chinese website!