current position:Home>Operation of strlen function

Operation of strlen function

2022-02-03 01:19:29 CSDN Q & A

include<stdio.h>
main()
{
char s[]="\n123\";
printf("%d,%d\n",strlen(s),sizeof(s));
}
Why is the output of this program after execution 5,6 Well




Refer to the answer 1:

First, this is a string containing five valid characters
\n, 1, 2, 3, \
And a '\0' Terminator
strlen Don't count \0 So it is 5
sizeof Calculation \0 So it is 6




Refer to the answer 2:



Refer to the answer 3:

strlen(s), Get the number of valid characters
sizeof(s), Get the number of occupied space , Including... At the end of the string '\0',
therefore sizeof(s) Than strlen(s), many 1




Refer to the answer 4:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202030119270480.html

Random recommended