When declaring a char array of length n, the value at [n] always is 0. Shouldn't it be a garbage value?
Code
#include <stdio.h>
int main() {
char arr[3];
arr[0] = 'a'; arr[1] = 'b'; arr[2] = 'c';
// Here arr[3] can be any garbage value.
// But it always appears to be 0. Why?
// Also arr[4], arr[5], arr[6]... are not 0,
// just some garbage as expected
printf("i\tch ascii\n");
int i;
for(i = 0; arr[i] != 0; i++) //Always breaks at i=3
printf("%d\t%c\t%d\n", i, arr[i], (int) arr[i]);
int more = i + 5;
for(; i<more; i++)
// I am intentionally going outside the bound
printf("%i\t%c\t%d\n", i, arr[i], (int) arr[i]);
return 0;
}
Expected output
What do you think the output will be? You may assume :
i ch ascii
0 a 97
1 b 98
2 c 99
3 N 78 ----> (This may or may not be 0)
4 � -103
5 N 78
6 � -125
7 � -100
Actual output
i ch ascii
0 a 97
1 b 98
2 c 99
3 0 ----> (Why is this always 0?)
4 � -103
5 N 78
6 � -125
7 � -100
Note: This does not happen with int/double/float arrays.