in Programming in C
651 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]);
}
}

 

in Programming in C
by
651 views

4 Comments

@srestha as per sir said, when you access a[12] - the behaviour in "Undefined" as per C standard.

0
0
undefined meant logic is faulty for compiler

Isnot it?

That is why it cannot able to generate genuine output

right?
0
0
yes , i think its right..
0
0

2 Answers

8 votes
8 votes
Best answer
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
by
0 votes
0 votes
Bcz in c array has a problem of boundary checking.

Related questions