The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
0 votes
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]);
}
}

 

asked in Programming by (171 points) | 107 views
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..

1 Answer

+3 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.
answered by Veteran (414k points)
selected by

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
49,814 questions
54,520 answers
188,354 comments
75,317 users