strlen() function is used in C language to calculate the length of a given string, excluding the null terminator: Declare a char array or pointer to store the string. Get the string. Pass a string pointer as an argument to the strlen() function. Store the returned length in a variable.
strlen() usage in C language
strlen() is a function in the C standard library , used to calculate the length of a given null-terminated string.
Syntax
size_t strlen(const char *str);
Parameters
str
: Pointer to null terminated string pointer. Return value
strlen() returns the length of the string, excluding the null terminator.
Usage
To use the strlen() function, follow these steps:
Example
#include
#include
int main() {
char str[] = "Hello World";
size_t length = strlen(str);
printf("字符串 '%s' 的长度为 %d\n", str, length);
return 0;
}
Output
字符串 'Hello World' 的长度为 11
Notes
The above is the detailed content of How to use strlen in c language. For more information, please follow other related articles on the PHP Chinese website!