Q:

Why the result is 0?

See these code as below:
#include<stdio.h>
#include<stdlib.h>

int main()
{
 char a[1000];
 int i;
 for(i = 1;i<1000;i++)
 {
  a[i] = 5;
 }

 printf("%d\n",strlen(a));

 return 0;
}

the result is 0 , why? Any explanation will be appreciated.

A:

You never assign or intialize a[0]. In this case, it just happened to be '\0', so strlen(a) returns 0.
It's worth noting that calling strlen on a here is undefined behaviour, as you're indirectly trying to read that uninitialized, garbage memory that is a[0] as the first step on the search to find a null terminator. Even setting that to 0 explicitly would still cause problems with it running off of the end of the array until it finds a 0, crashes, or blows up.

