is that char null terminator is including in the length count

后端 未结 5 1834
春和景丽
春和景丽 2020-12-24 05:37
#include 

int main(int argc, char *argv[]) {
   char s[]=\"help\";
   printf(\"%d\",strlen(s));  
}

Why the above output is 4, isn

5条回答
  •  自闭症患者
    2020-12-24 05:53

    strlen() does not count the number of characters in the array (in fact, this might not even be knowable (if you only have a pointer to the memory, instead of an array). It does, as you have found out, count the number of characters up to but not including the null character. Consider char s[] = {'h','i','\0','t','h','e','r','e'};

提交回复
热议问题