is that char null terminator is including in the length count

后端 未结 5 1833
春和景丽
春和景丽 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 06:13

    strlen(const char* ptr) returns the length of the string by counting the non-zero elements starting at until reaches zero. So '\0' doesn't count.

    I recommend that you consult the reference like link for questions like this.

    It's clearly stated as:

     A C string is as long as the number of characters between the beginning 
     of the string and the terminating null character (without including the
     terminating null character itself).
    

提交回复
热议问题