Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

C Quiz – 112 | Question 1

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Pick the best statement for the below program:




#include "stdio.h"
  
int main()
{
 struct {int a[2];} arr[] = {{1},{2}};
  
 printf("%d %d %d %d",arr[0].a[0],arr[0].a[1],arr[1].a[0],arr[1].a[1]);
  
 return 0;
}


(A) Compile error because arr has been defined using struct type incorrectly. First struct type should be defined using tag and then arr should be defined using that tag.
(B) Compile error because apart from definition of arr, another issue is in the initialization of array of struct i.e. arr[].
(C) Compile error because of initialization of array of struct i.e. arr[].
(D) No compile error and it’ll print 1 2 0 0
(E) No compile error and it’ll print 1 0 2 0


Answer: (E)

Explanation: Here, struct type definition and definition of arr using that struct type has been done in the same line. This is okay as per C standard. Even initialization is also correct. The point to note is that array size of arr[] would be 2 i.e. 2 elements of this array of this struct type. This is decided due to the way it was initialized above. Here, arr[0].a[0] would be 1 and arr[1].a[0] would be 2. The remaining elements of the array would be ZERO. correct answer is E.


Quiz of this Question

My Personal Notes arrow_drop_up
Last Updated : 28 Jun, 2021
Like Article
Save Article
Similar Reads