107 views

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]);
}
}

0
Check in other compiler

I am getting runtime error
0
mam, check the answer by Arjun sir
0

yes

@Shaik Masthan

that means in some cases a[11] array can take even 20 elements

Why compiler works so faulty? 😓

0

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

0
undefined meant logic is faulty for compiler

Isnot it?

That is why it cannot able to generate genuine output

right?
0
yes , i think its right..

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

1
+1 vote