C Programming - Arrays - Discussion

Discussion Forum : Arrays - General Questions (Q.No. 1)
1.
What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of array?
The element will be set to 0.
The compiler would report an error.
The program may crash if some important data gets overwritten.
The array size would appropriately grow.
Answer: Option
Explanation:

If the index of the array size is exceeded, the program will crash. Hence "option c" is the correct answer. But the modern compilers will take care of this kind of errors.

Example: Run the below program, it will crash in Windows (TurboC Compiler)

#include<stdio.h>

int main()
{
    int arr[2];
    arr[3]=10;
    printf("%d",arr[3]);
    return 0;
}

Since C is a compiler dependent language, it may give different outputs at different platforms. We have given the Turbo-C Compiler (Windows) output.

Please try the above programs in Windows (Turbo-C Compiler) and Linux (GCC Compiler), you will understand the difference better.

Discussion:
27 comments Page 3 of 3.

Arunesh said:   1 decade ago
I have tried the program but the program gives me the out put 10 .
I have tried on indiabix online compiler

#include<stdio.h>
int main()
{
int arr[2];
arr[3]=10;
printf("%d",arr[3]);
return 0;
}

please explain how?

Cybog said:   1 decade ago
Even if some value is already present at that location, it will be overwritten. So if some important value like count or status is stored there, it may lead to unexpected o/p or program crash.

Vishwas said:   1 decade ago
For character array there is an indication of end of the string by null char, but in integer array there is no such end, if there is an free space at address arr[5] in RAM it will get allocated by value 6.

Raj said:   1 decade ago
The code which I tried in turbo C is

#include<stdio.h>
int main()
{
int arr[3]={1,2,3};
arr[5]=6;
printf("%d",arr[5]);
return 0;
}

This program prints the output as 6.

Can anyone explain please ?

Gajendra gayakwad said:   1 decade ago
Because in c language bound checking is performed by compiler array size always be given by programmer side.

Sundar said:   2 decades ago
Hi All,

I have tested the above program in Turbo C, it crashes at run time as explained (as given in Option C).

In GCC it shows the output as '10'. Here the compiler takes care of this kind of situation (as given in Option D).

And I have tested the same concept in Java as given below:

public class Test
{
public static void main(String [] args)
{
int arr[] = new int[2];

arr[3] = 10;

System.out.print("The output is : " + arr[3]);
}
}

It gives the following Run time error (as given in Option C):

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Test.main(Test.java:8)

Therefore, I conclude that there are more possibilities of program crash or termination. So option C is correct.

Vaishnavi said:   2 decades ago
When I tried the program in the compiler an error namely "invalid initialization"appeared. So the compiler has thrown an error.

Then why could not B option be the correct answer. Could anyone please explain me.


Post your comments here:

Your comments will be displayed after verification.