The role of the strlen() function: count the length of a given string
Function: Count the number of characters in the string str, excluding '\0'
That is to say, when the character '\0' is encountered , the strlen() function will end (\0 is not included in the length)
Note: The difference between 0 and '0' The value of '0' is 48,
The value of '\0' is 0, and the end of encountering '\0' is the end of encountering 0
But it's not the end of case '0'!
Example 1:
#include<stdio.h>
#include<string.h>
int main() {
char s[]="hebeisheng\0\n";
printf("%d",strlen(s));
return 0;
}
The value of '\0' is 0, and the end of encountering '\0' is the end of encountering 0
So only the length before \0 is counted
Example 2:
If we add '\0' to the string
#include<stdio.h>
#include<string.h>
int main() {
char s[]="hebeisheng'\0'\n";
printf("%d",strlen(s));
return 0;
}
The final result is 11, just add the single quotation mark in front of '\0', encountering \0 will still end the strlen function
Example 3:
When using the strlen function: you should pay attention to escape characters, a set of escape characters counts as a length
Attachment: escape character table