How to determine the length of characters in the C language?
In C language, you can use the strlen function to retrieve the length of a string. The prototype of this function is as follows:
size_t strlen(const char *s);
Among them, the parameter s is a pointer to a null-terminated string. The function returns the length of the string, not including the null terminator. For example, the following code shows how to use the strlen function to get the length of a string:
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("请输入一个字符串:");
scanf("%s", str);
size_t len = strlen(str);
printf("字符串的长度是:%zu\n", len);
return 0;
}
Please note that the strlen function is only suitable for null-terminated strings. If the parameter passed is not null-terminated, its behavior is undefined. To ensure safety, make sure the string is null-terminated before using the strlen function.