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?
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 2 of 3.
Nar singh yadav said:
1 decade ago
What does the following declaration means int (*ptr) [10];?
Shiva said:
1 decade ago
I have tried in my compiler but came out of editor? Anyone explain?
Saloni said:
1 decade ago
I tried it on Turbo C and get result 10.
Difu said:
1 decade ago
#include<stdio.h>
#define SQR(x) (x*x)
int main(){
int a,b=3;
a= SQR(b+2);
printf("%d",a);
return 0;
}
What is the output of this program? why not 25?
#define SQR(x) (x*x)
int main(){
int a,b=3;
a= SQR(b+2);
printf("%d",a);
return 0;
}
What is the output of this program? why not 25?
Pritam said:
1 decade ago
I write the same program in Turbo C and Linux. It shows me answer 10. Compiler not given any type of error. Can you explain me?
Vasuvandan said:
1 decade ago
Sometimes it throws error and other times it assigns value even array bound exceeds.
Priyanka kumari said:
1 decade ago
I write the same program but it shows me answer 10. I executed in the compile given below and execute it it also shows result 10. Then how can we decide appropriate option? it creates ambiguity.
Sujeet kumar said:
1 decade ago
When I tried the program in the compiler an error namely "invalid initialization"appeared. So the compiler has thrown an error.
Naresh said:
1 decade ago
What is a magic matrix?
Swathi gs said:
1 decade ago
In GCC it shows the output as '10'. Here the compiler takes care of this kind of situation (as given in Option D).
please explain this.
please explain this.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers