Arrays

Q1: Which of the following statements are correct about the program below?
#include<stdio.h>

int main()
{
    int size, i;
    scanf("%d", &size);
    int arr[size];
    for(i=1; i<=size; i++)
    {
        scanf("%d", arr[i]);
        printf("%d", arr[i]);
    }
    return 0;
}

A The code is erroneous since the subscript for array used in for loop is in the range 1 to size.

B The code is erroneous since the values of array are getting scanned through the loop.

C The code is erroneous since the statement declaring array is invalid.

D The code is correct and runs successfully.

ANS:C - The code is erroneous since the statement declaring array is invalid.

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().



img not found
img

For help Students Orientation
Mcqs Questions

One stop destination for examination, preparation, recruitment, and more. Specially designed online test to solve all your preparation worries. Go wherever you want to and practice whenever you want, using the online test platform.