I know that this question had been asked a million times, but I still get confused. I intentionally make my string size equal to the char number in it:
int main()
{
int i = 0;
char str[5] = "check";
while((str[i] != '\0') && (i != 10)){ // (i != 10) aborts the func
printf("str[i] = %c\n", *(str+i));
printf("i = %d\n", i);
i++;
}
}
The output is:
str[i] = c
i = 0
str[i] = h
i = 1
str[i] = e
i = 2
str[i] = c
i = 3
str[i] = k
i = 4
Why does it still store null character at the end of it, despite there is no space allocated? Does it depend on the compiler, or do all of them work the same way?