This is a C program check array bounds while inputing elements into the array.
This is a C program which implements an array and checks array bounds while inputing elements into the array.
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.
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.
* C Program to Check Array bounds while Inputing Elements into the Array
int array, b, c;
for (b = 0; b < 10 && (scanf("%d", &c)); b++)
array[b] = c;
for (b = 0; b < 15; b++)
printf("%d ", array[b]);
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.
12 23 56 12 14 23 12 23 56 12 14 23 6 134513824 0 -1081194040 11672807 1 -1081193996 -1081193988 -1216161720
Sanfoundry Global Education & Learning Series – 1000 C Programs.