- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Accessing array out of bounds in C/C++
In a language such as Java, an exception such as java.lang.ArrayIndexOutOfBoundsException may occur if an array is accessed out of bounds. But there is no such functionality in C and undefined behaviour may occur if an array is accessed out of bounds.
A program that demonstrates this in C is given as follows.
Example
#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; }
Output
The output of the above program is as follows.
The elements of array : 1 2 3 4 5 32765
Now let us understand the above program.
The array arr has assigned values only till subscript 4. So when the array elements are printed, arr[5] results in a garbage value. The code snippet that shows this is as follows.
int arr[] = {1,2,3,4,5}; printf("The elements of array : "); for(int i = 0; i<6; i++) printf(" %d",arr[i]);
- Related Articles
- Why accessing an array out of bounds does not give any error in C++?
- What is out of bounds index in an array - C language?
- How to capture out of array index out of bounds exception in Java?
- Get bounds of a C# three-dimensional array
- How to handle Java Array Index Out of Bounds Exception?
- Explain the concept of Uninitialized array accessing in C language
- How do you use ‘foreach’ statement for accessing array elements in C#
- Accessing Attributes and Methods in C#
- How do you use a ‘for loop’ for accessing array elements in C#?
- Accessing Array Elements in Perl
- Last element of vector in C++ (Accessing and updating)
- Explain the concept of pointer accessing in C language
- Explain the accessing of structure variable in C language
- Accessing inner element of JSON array in MongoDB?
- Program to Find Out Median of an Integer Array in C++

Advertisements