Which of the following statements are correct about the program below?
#include<stdio.h>
void main()
{
int size, i;
scanf("%d", &size);
int arr[size];
for(i=1; i<=size; i++)
{
scanf("%d", arr[i]);
printf("%d", arr[i]);
}
}
#include<stdio.h>
void main()
{
int size, i;
scanf("%d", &size);
int arr[size];
for(i=1; i<=size; i++)
{
scanf("%d", arr[i]);
printf("%d", arr[i]);
}
}
A. The code is erroneous since the statement declaring array is invalid.
B. The code is erroneous since the subscript for array used in for loop is in the range 1 to size.
C. The code is correct and runs successfully.
D. The code is erroneous since the values of array are getting scanned through the loop.
E. None of these
Answer: Option A
Solution(By Examveda Team)
The statement int arr[size]; produces an error, because we cannot initialize the size of array dynamically. Constant expression is required here.
Example: int arr[10];
One more point is there, that is, usually declaration is not allowed after calling any function in a current block of code. In the given program the declaration int arr[10]; is placed after a function call scanf().
Join The Discussion
Comments ( 5 )
Related Questions on Arrays and Strings
No error, It works on Code block 20.03.
It has no error
No,those statements work without even generating warnings
& sign is also missing.
HERE WE WILL GET GARBAGE, AS VALUES OF ARRAY ARE NOT SCANNED BY &.......SO NO USE OF IT.