C Program to Check Array bounds while Inputting Elements into the Array

This is a C program check array bounds while inputing elements into the array.

Problem Description

This is a C program which implements an array and checks array bounds while inputing elements into the array.

Problem Solution

1. Create an array of some fixed capacity.
2. Run a for loop more than the array capacity times, inputing array elements.
3. Print array elements using for loop, where the elements entered by user are printed as it is, but rest printed numbers will be garbage values.

Program/Source Code

Here is source code of the C Program to check array bounds while inputing elements into the array. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1.     /*
  2.      * C Program to Check Array bounds while Inputing Elements into the Array
  3.      */
  5.     #include <stdio.h>
  6.     int main(void)
  7.     {
  9.         int array[5], b, c;
  10.         for (b = 0; b < 10 && (scanf("%d", &c)); b++)
  11.                 array[b] = c;
  13.         for (b = 0; b < 15; b++)
  14.         	printf("%d ", array[b]);
  16.         return 0;
  17.     }
Program Explanation

1. Declare an array of some fixed capacity, lets say 5.
2. Using a for loop, take input from users each array element. Loop in this program runs more than the size of the array.
3. As the expression array[position] equals *(array + position), therefore the loop continues to go beyond array size and defining values at the required position.
4. At the time of printing, we have executed the for loop 15 times, whereas the size of the array is 5 and we have defined the elements from start position of the array to the 10th position of the array.
5. Therefore, we get first 10 numbers as whatever we entered, but after that since we did not entered any number, so already stayed garbage values are written.

Runtime Test Cases
12 23 56 12 14 23 6 134513824 0 -1081194040 11672807 1 -1081193996 -1081193988 -1216161720

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.