ในภาษาเช่น Java อาจเกิด exception java.lang.ArrayIndexOutOfBoundsException ถ้า access array out of bounds แต่ใน C ไม่มี functionality นี้ undefined behaviour อาจเกิดขึ้น
โปรแกรมสาธิตใน C:
ตัวอย่าง
#include <stdio.h> int main() { int arr[] = {1,2,3,4,5}; printf("The elements of array : "); for(int i = 0; i<6; i++) printf(" %d",arr[i]); return 0; }ผลลัพธ์
The output of the above program is as follows.
The elements of array : 1 2 3 4 5 32765
เข้าใจโปรแกรมข้างต้น:
Array arr มีค่าจนถึง subscript 4 ตอนพิมพ์ arr[5] ได้ garbage value Code snippet:
int arr[] = {1,2,3,4,5}; printf("The elements of array : "); for(int i = 0; i<6; i++) printf(" %d",arr[i]);