In C language, if initialize an array like this:
int a[5] = {1,2};
then all the elements of the array that are not initialized explicitly will be initialized implicitly with zeros.
But, if I initialize an array like this:
int a[5]={a[2]=1};
printf("%d %d %d %d %d\n", a[0], a[1],a[2], a[3], a[4]);
output:
1 0 1 0 0
I don't understand, why does a[0] print 1 instead of 0? Is it undefined behaviour?
Note: This question was asked in an interview.