Discussion
Pick the best statement for the below program:
#include "stdio.h"
int main()
{
struct {int a[2], b;} arr[] = {[0].a = {1}, [1].a = {2}, [0].b = 1, [1].b = 2};
printf("%d %d %d and ",arr[0].a[0],arr[0].a[1],arr[0].b);
printf("%d %d %d\n",arr[1].a[0],arr[1].a[1],arr[1].b);
return 0;
}
Answer: C
Correct answer: No compile error and two elements of arr[] would be defined and initialized. Output would be “1 0 1 and 2 0 2”.
The initialiser uses designated initialisers with array indices: [0].a = {1}, [1].a = {2} and so on. This is valid C99, and it sizes arr at two elements, since index 1 is the highest named. Everything not explicitly initialised is zero-filled, so a[1] is 0 in both - giving 1 0 1 and 2 0 2.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development