658 views
0 votes
0 votes

i write this program, during initialization of array i given the size as 11 means the number of elements stored in an array is 11.

as we know array is not assigned a value of index 12 and above. but in in the program array a is initialized of index from 0 to 19,my question is how the array is initialized with 20 elements although i specified size is 11.

i run in 64 bit system

#include<stdio.h>
int main()
{
	int a[11],i;
for(i=0;i<20;i++)
{
	a[i]=i+1;
}

for(i=0;i<20;i++)
{
	printf("%d\n",a[i]);
}
}

 

2 Answers

Best answer
8 votes
8 votes
C is a very flexible language as it cares mainly for "performance" and "simplicity for compiler". So, if you do

int a[11];

C standard only says that you are allocated 11* sizeof(int) bytes of memory which can be accessed through the variable 'a'.

'a' being a local variable, C language does not guarantee initialization -- its value is GARBAGE. It would have been initialized to "0" had 'a' been a static or global variable.

Now, when you access a[12] - the behaviour in "Undefined" as per C standard. You can get any output and a good programmer should not care for it. Depending on the compiler implemnetation and the environment on which the program runs you can get any output including Runtime error.
selected by

Related questions

1 votes
1 votes
1 answer
1
Mrityudoot asked Feb 2
248 views
In what cases does an uninitialized array have values = 0 and for which cases does it have values = garbage values. How to differentiate?
5 votes
5 votes
1 answer
2
6 votes
6 votes
1 answer
4