-3

Recently I started learning C by myself, so might be a bit newbie question. I have compiled the following commands:

#include <stdio.h>

int main()
{
  int arr[5]={0};
  int arr2[5]={0};
  printf("%d\n",arr[5]); //here output: 2130567168
  printf("%d\n",arr2[5]); //here output: 0
  return 0;
}

can anybody explain the reason for the different outputs?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
yank1
  • 11
  • 1
  • 6
    you do realise that you are accessing your arrays out of bounds? you only allocated space for 5 ints for both yet you are trying to access a sixth one – UnholySheep Oct 17 '14 at 18:25
  • possible duplicate of [How dangerous is it to access an array out of bounds?](http://stackoverflow.com/questions/15646973/how-dangerous-is-it-to-access-an-array-out-of-bounds) – Deduplicator Oct 17 '14 at 18:27

5 Answers5

3

Both printfs produce undefined behavior, because they are accessing elements past the end of their corresponding arrays. The valid range for indexes of C arrays is zero through length-1, so for both arrays in your program that would be 0..4, inclusive.

Accessing element at index 5 is past the end of the array, so different compilers can produce different behavior, ranging from printing garbage to crashing.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

You are acessing data which is outside of the declared range. The index range of your array which has been declared as arr[5], goes from 0...4, but you want to access arr[5], which is neither defined nor allocated. Thus you get random answers depending on the value of the memory "cell" you want to access, in this case 0 and the other number.

arc_lupus
  • 3,942
  • 5
  • 45
  • 81
2

This is undefined behavior. C does not perform bound checking on array.

ani627
  • 5,578
  • 8
  • 39
  • 45
2

In the future, please enable warnings on your compiler. For example for GCC, this would be -Wall.

main.cpp:7:29: warning: array subscript is above array bounds [-Warray-bounds]
         printf("%d\n",arr[5]); //here output: 2130567168
                             ^
main.cpp:8:30: warning: array subscript is above array bounds [-Warray-bounds]
         printf("%d\n",arr2[5]); //here output: 0

Note that the C standard says this is undefined behavior:

§J.2

1 The behavior is undefined in the following circumstances:

— An array subscript is out of range, even if an object is apparently accessible with the given subscript (as in the lvalue expression a[1][7] given the declaration int a[4][5]) (6.5.6).

Receive your usual lecture about undefined behavior.

0

The behavior of this code in C is described in section 6.7.8.21 of the C specification (Online draft for c specs): for the elements that don't have a specified value, the compiler initializes pointers to NULL and arithmetic types to zero (and recursively applies this to aggregates).

Please refer the above document to know . also i am not posting answer directly here as You are newbie as i don't wanna spoon -feed you. Happy Learning. .

coder3521
  • 2,608
  • 1
  • 28
  • 50