#include <stdio.h>
#include <assert.h>
int strlen(const char* s)
{
return assert(s),(*s ? strlen(s+1 ) + 1 : 0);
}
int main()
{
printf("len = %d \n ",strlen("OK"));
printf("len = %d \n ",strlen(NULL));
}
strlen 函数遇到\0就停止计算字符串的长度,返回的是字符串的实际长度,所以不包括结尾的 \0 终止符。为了保证有足够的空间存储所有字符,我们需要在额外 +1。