I wrote a C program as follows and compiled it using GCC version 4.6.3.
#include <stdio.h>
int main(void)
{
char array1[7] = "network"; // no space for \0 in array here
char array2[5] = "network"; // even here, no space for \0 in array
printf("1. %s\n",array1);
printf("2. %s\n",array2);
return 0;
}
On compilation :-
warning: initializer-string for array of chars is too long [enabled by default]
the output of program is :-
1. network
2. netwo
In output for array2 :- netwo+unprintable character. The non-printable character having hex value 7F.
My question is:-
- While printing value of array1, why doesn't it print garbage value after printing "network" as in case of printing array2.
This doubt is supported by the fact that there is no NULL terminator in array1 nor in array2, so why garbage value only after array2's output?
So, does GCC check array bounds?