IndiaBIX.com
Arithmetic Aptitude Data Interpretation
Logical Reasoning Verbal Reasoning Non Verbal Reasoning
General Knowledge
Sudoku Number puzzles Missing letters puzzles Logical puzzles Playing cards puzzles Clock puzzles
C Programming C# Programming Java Programming
Networking Database Questions Computer Science Basic Electronics Digital Electronics Electronic Devices Circuit Simulation Electrical Enigneering Engineering Mechanics Technical Drawing
Placement Papers Group Disucssion HR Interview Technical Interview Body Language
Aptitude Test Verbal Ability Test Verbal Reasoning Test Logical Reasoning Test C Programming Test Java Programming Test Data Interpretation Test General Knowledge Test
Data Structures Operating Systems Networking DATABASE Database Basics SQL Server Basics SQL Server Advanced SQL Server 2008 JAVA Core Java Java Basics Advanced Java UNIX Unix File Management Unix Memory Management Unix Process Managemnt C Interview Questions The C Language Basics .NET Interview Questions .NET Framework ADO.NET ASP.NET Software Testing

C Programming - Arrays - Discussion

@ : Home > C Programming > Arrays > General Questions - Discussion

Read more:

"Time and tide wait for none."
- Alexander Pope
1. 

What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of array?

[A]. The element will be set to 0.
[B]. The compiler would report an error.
[C]. The program may crash if some important data gets overwritten.
[D]. The array size would appropriately grow.

Answer: Option B

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.


Vaishnavi said: (Mon, Aug 2, 2010 12:46:33 PM)    
 
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.

Sundar said: (Tue, Aug 3, 2010 12:48:56 AM)    
 
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.

Gajendra Gayakwad said: (Sat, Sep 3, 2011 03:52:16 PM)    
 
Because in c language bound checking is performed by compiler array size always be given by programmer side.

Raj said: (Wed, Sep 7, 2011 06:36:23 PM)    
 
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 ?

Vishwas said: (Fri, Jan 6, 2012 09:59:16 AM)    
 
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.

Write your comments here:
Name *:     Email:


© 2008-2011 by IndiaBIX™ Technologies. All Rights Reserved | Copyright | Terms of Use & Privacy Policy

Advertise     Contact us: info@indiabix.com     Follow us on twitter!