What will be the output of this code?
union Data { int x; char y; };
Data d = {65};
d.y = 'A';
printf("%d", d.x);
Modifying y changes the same memory location as x. Only the last byte of x is affected, leading to unpredictable output.
Which of these is a valid declaration of a structure variable using dot operator immediately after struct definition?
Both methods are valid - A uses named struct tag with variable declaration, C uses anonymous struct with variable declaration.
What is the issue with bit fields in structures regarding portability?
Bit field allocation order (left-to-right or right-to-left) varies across compilers, affecting portability.
In a structure with flexible array members, which statement is INCORRECT?
struct FlexArray { int len; int arr[]; };
Flexible array members require dynamic allocation; the structure itself cannot be allocated on the stack with a defined size for the array.
What happens when you access a union member that wasn't the last one to be written?
All union members share the same memory. Accessing a member not recently written gives bitwise interpretation of that memory.
Advertisement
Which of the following correctly demonstrates self-referential structure (for linked list)?
In C, self-referential structures require the struct keyword in pointer declarations within the structure definition.
What is the output of the following?
struct S { int a:3; int b:3; int c:3; };
printf("%zu", sizeof(struct S));
Bit field packing is implementation-defined. Size depends on compiler's bit field allocation strategy.
When comparing nested structures, which access method is INVALID?
struct Address { char city[20]; };
struct Person { Address addr; };
Person p;
p is a structure variable (not pointer), so arrow operator (->) cannot be used. Only dot operator works.
Which structure feature is most suitable for implementing a state machine with limited states?
Bit fields efficiently store boolean/limited-range states using minimal memory, ideal for state machines.
What is the primary use case for unions in embedded systems?
Unions enable type punning and efficient hardware register representation where different interpretations of same memory are needed.
When structure is passed by value to a function, which of these is true?
Pass by value creates a complete copy of the structure on the function's stack frame.
What is the correct way to dynamically allocate an array of structures?
struct Student { int roll; char name[50]; };
Correct allocation requires pointer declaration, proper cast, and multiplying n with sizeof(struct Student).
In competitive programming, when should you use union instead of struct?
Unions are optimal for memory-constrained scenarios and hardware programming where type casting is needed.
What will be the output of this code?
struct X { int a; float b; };
struct Y { struct X x; int c; };
printf("%zu", sizeof(struct Y));
Nested struct X: int(4) + float(4) = 8 bytes. Y: struct X(8) + padding(0) + int(4) = 12, but with overall alignment it becomes 16 bytes.
What is the size of the following structure on a 32-bit system?
struct Point { char c; int x; };
Due to structure padding/alignment, char (1 byte) + 3 bytes padding + int (4 bytes) = 8 bytes total.
Which of the following statements about unions is correct?
Union members share the same memory location, and the union size equals the size of its largest member.
Consider: union Data { int i; float f; char c; }; What is the size of this union?
The size of a union equals the size of its largest member. Here, int is 4 bytes (largest), so union size is 4 bytes.
Which feature of structures makes them suitable for implementing linked lists?
Self-referential pointers (a structure containing a pointer to itself) are essential for creating linked list nodes.
How do you access a member of a structure through a pointer in C?
The arrow operator (->) is used to access structure members through a pointer. The dot operator is used with direct variables.
What happens if you attempt to assign one structure variable to another of the same type?
In C, you can assign one structure variable to another of the same type, and all members are copied (shallow copy).