Home  >  Article  >  Backend Development  >  What is the usage of strlen function in c language

What is the usage of strlen function in c language

coldplay.xixi
coldplay.xixiOriginal
2020-08-19 15:23:4329400browse

The usage of the strlen function in c language is a function of the C language library, included in [string.h]. It is used to count the number of elements in a string, that is, counting from the head of the array until a character is encountered. until the string terminator [\0], the counting result does not include [\0].

What is the usage of strlen function in c language

C language strlen function usage:

strlen is a C language library function, included in string.h, The function is to count the number of elements in a string (character array), that is, counting from the head of the array until the string end character \0 is encountered. The counting result does not include \0.

The declaration is:

unsigned int strlen(char *s);

For example, defining a string:

char *p = "this is for testing";

Call

strlen(p);

The returned result is the number of elements in p, which is 19.

It should be noted that strlen is not absolutely safe. If the terminator \0 does not exist within the legal range of the incoming character array, then the strlen function will continue to access the array beyond the range, that is An out-of-bounds access occurred.

So when using strlen, programmers must confirm that dao in the parameter character array contains the \0 value, otherwise unpredictable consequences will occur.

Related learning recommendations: C video tutorial

The above is the detailed content of What is the usage of strlen function in c language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn