2

Here is an Array program.

Practice.c

#include <stdio.h>

void main()
{
  int i,num[10]={10,20,30,40,50};

  for(i=0;i<15;i++)
  {
    printf("\nnum[%d] =%d",i,num[i]);
  }
}

Following is the output of the Program.

num[0] =10
num[1] =20
num[2] =30
num[3] =40
num[4] =50
num[5] =0
num[6] =0
num[7] =0
num[8] =0
num[9] =0
num[10] =10
num[11] =0
num[12] =2686711
num[13] =2686820
num[14] =4199443


Now I have 2 questions:

1- Why is num[5] to num[9] displayed there values as 0?

2- Why num[10] to num[14] are displaying these values, when I only have declared the length as int num [10] and not as int num [15], shouldn't the program be displaying an error while running or compiling?

4
  • 2
    Bounds-checking isn't a feature built into C, or guaranteed by C. Why do you expect it? Commented Jun 26, 2014 at 1:37
  • Did u deleted your answer right now @Charles Duffy Commented Jun 26, 2014 at 1:38
  • 1
    See e.g. here. Or one of the many questions here or elsewhere in the web about array initialization and out-of-bounds access. Commented Jun 26, 2014 at 1:39
  • I am using gcc, and every time I tried to check that whether it was my luck or not, the result was same. Uninitialized but declared arrays were showing there values as 0. Commented Jun 26, 2014 at 1:40

1 Answer 1

4

Here is what your program is doing:

int num[10] = {10,20,30,40,50}

The above line tells the compiler to create an array of ten integers and initialize the first five values (indices 0-4) to the ones you provided. The remainder of the values (indices 5-9) are automatically initialized to zero per the C99 specification.

num[9] # This value was specifically allocated and initialized.
num[10] # This value was neither allocated nor initialized but it exists anyway.

When you say num[9] you are telling the compiler to access the integer that is nine integers away from the start of the array called num, in fact, it's almost exactly the same as saying *(num+(int*)9).

It's pure luck that you can access the 10th through 15th values - you didn't ask the compiler to allocate that space but it's there anyway, and the computer doesn't prevent you from looking at it.

In summary, you have just discovered the fact that the C language doesn't prevent you from accessing values that you have not initialized or specifically asked to allocate!

Sign up to request clarification or add additional context in comments.

16 Comments

C99/C11 p.6.7.9 (19) and (21). The remaining fields are initialized to zero.
@VSP: the reason it never displays an error is because the compiler is always allowing you to access some large number of integers beyond the values you specifically allocated. Try changing i<15 to i<1000000 and see how far it will let you read!
It's pure luck should be it's pure undefined behaviour and you are looking for troubles
@VSP: as another user pointed out, this behavior is technically "undefined", meaning that the C language specification doesn't mandate specific rules for the compiler to adhere to, so it is free to make up its own.
Is this a typo: num[10] # This value was specifically allocated and initialized.? The last index allocated was num[9]
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.