Structures and unions look easy in notes, but questions in GATE, PSU and placement papers punish two small habits: not counting bytes carefully, and confusing what a union stores with what a structure stores. Work each question below as a timed attempt: read the stem, pick an option, then check your reasoning against the byte counts.
Structures versus unions: the one sizing rule most questions turn on
A structure reserves memory for every member, so its size is their sum when alignment is ignored. A union reserves memory only for its largest member because all members share an address. Writing one overwrites the shared storage.
typedef does not create storage or a variable. It only gives an existing type a new name. For a full walkthrough from declaration syntax to memory layout, use the C Programming Course.
Declaring and initialising a structure
A declaration has to be legal before its size is worth counting, and aggregate initialisation fills members strictly in declaration order.
Question 1
#include<stdio.h>
typedef struct stu{
char * name;
int roll;
}s;
int main(){
s arr[2]={{"raja",10},{"rani",11}};
printf("%s %d",arr[0]);
return 0;
}A. raja rani
B. rani 11
C. raja 10
D. Compilation Error
Answer: C. raja 10.
arr[2] is an array of two stu structures, initialised member by member. arr[0] holds the name "raja" and roll number 10. The printf call reads those two members in order, so %s prints the name and %d prints the roll number. Aggregate initialisation fills members from left to right in declaration order, which is why the first pair lands in arr[0]. Strictly, handing a whole structure to printf like this is not something the C standard defines; the option set expects the usual compiler behaviour, where the two members arrive in the order they were declared.
Question 2
#include <stdio.h>
struct data{
int p;
static int q;
};
void main(){
printf("%d", sizeof(struct data));
}Assume that an integer occupies 4 bytes.
A. Compile error
B. 8
C. Run time error
D. 1
Answer: A. Compile error.
A structure member cannot be declared static in C. Storage-class specifiers such as static are not allowed inside a structure definition, so this program never compiles. Option B is the trap for anyone who adds 4 + 4 before checking whether the declaration itself is legal.
What typedef really does with structures
Renaming a type is not the same as declaring an object, fixing its size or initialising it. typedef does only the renaming, and the three traps here all turn on that difference.
Question 3
What is the purpose of typedef in C when used with structures?
A. To define a new data type based on an existing one
B. To declare a structure
C. To specify the size of a structure
D. To initialize a structure
Answer: A. To define a new data type based on an existing one.
typedef creates an alias for an existing type. For example, typedef struct node Node; lets you write Node n; instead of struct node n;. It does not allocate storage, decide a structure's size or initialise an object.
Question 4
#include <stdio.h>
int main() {
typedef int *i;
int j = 10;
i a = &j;
printf("%d", *a);
getchar();
return 0;
}A. 10
B. 0
C. 1
D. error
Answer: A. 10.
typedef int *i; makes i an alias for pointer to int, so i a declares an int*. The assignment stores the address of j, and *a reads its value, 10. The pointer is part of the alias, which is the trap.
Question 5
typedef struct node {
int data;
struct node *next;
} node;
node *ptr;Which one of the following correctly creates a new node?
A. ptr= (node*)malloc(sizeof(node*))
B. ptr=(node)malloc(sizeof(node))
C. ptr=(node*)malloc(sizeof(node))
D. None of the above
Answer: C. ptr=(node*)malloc(sizeof(node)).
The allocation must reserve a complete node, so use sizeof(node), not sizeof(node*). The result in these options must be a node*, making C correct. A reserves only pointer-sized storage, while B casts the address to a non-pointer type.
Self-referential structures and building a node
This form writes the type alias separately, but the allocation rule remains the same.
Question 6
struct node {
int data;
struct node * next;
};
typedef struct node NODE;
NODE *ptr;Which of the following c code is used to create a new node?
A. ptr=(NODE*)malloc(sizeof(NODE));
B. ptr=(NODE*)malloc(NODE);
C. ptr=(NODE*)malloc(sizeof(NODE*));
D. ptr=(NODE)malloc(sizeof(NODE));
Answer: A. ptr=(NODE*)malloc(sizeof(NODE)).
malloc needs a byte count, and sizeof(NODE) supplies the complete node size. B passes a type instead of a size. C allocates only pointer-sized space, and D casts to a value type. The pattern is (TYPE*)malloc(sizeof(TYPE)).
Sizing a structure that contains a union
Once a union sits inside a structure, the total size, each member's offset and pointer arithmetic across an array of that structure all have to agree.
Question 7
struct {
short s[5];
union {
float y;
long z;
} u;
} t;Assume that the objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes, respectively. The memory requirement for variable t, ignoring alignment consideration, is
A. 22 bytes
B. 18 bytes
C. 14 bytes
D. 10 bytes
Answer: B. 18 bytes.
First, short s[5] requires 5 x 2 = 10 bytes. The union takes the size of its largest member, so max(4, 8) = 8 bytes. Because alignment is ignored, the complete structure needs 10 + 8 = 18 bytes. Option A comes from incorrectly adding the two overlapping union members as 4 + 8.

Question 8
typedef struct {
union {
unsigned char a;
unsigned short b;
} U;
unsigned char c;
} S;
S B[10];
S *p = &B[4];
S *q = &B[5];
p->U.b = 0x1234; /* structure S takes 32 bits */If M is the value of q - p and N is the value of ((int)&(p->c)) - ((int)p), then (M, N) is:
A. (1, 1)
B. (3, 2)
C. (1, 2)
D. (4, 4)
Answer: C. (1, 2).
The union uses max(1, 2) = 2 bytes at offset 0, so member c occupies one byte at offset 2. The comment fixes sizeof(S) at 32 / 8 = 4 bytes, leaving byte 3 as padding. Pointer subtraction on S* counts elements, so with p at B[4] and q at B[5], M = 5 - 4 = 1. The offset gives N = 2 - 0 = 2, hence (M, N) = (1, 2); D confuses structure size with an element difference and a member offset.
![Byte layout of struct S: a 2-byte union at offset 0, member c at offset 2 and one padding byte, with B[4] and B[5] one element apart.](https://cdn.knowledgegate.ai/blog-assets/blog_asset_1784173744431_5vc2zg.jpg)
Union basics and how a union differs from a structure
Question 9
What is the keyword used to define a Union?
A. union
B. structure
C. enum
D. both a and b
Answer: A. union.
The C keyword is union. A structure and a union can contain similar member declarations, but their storage rules are different.
Feature | struct | union |
|---|---|---|
Memory reserved | Sum of all members | Size of the largest member only |
Members at any instant | All hold valid values | Only one member is valid at a time |
Typical use | Group related fields | Save memory or interpret the same bytes differently |
Passing a structure: by value versus by pointer
A function that takes a structure by value works on a copy, so the caller's object is untouched. Only a pointer parameter reaches the original.
Question 10
#include <stdio.h>
typedef struct {
char *a;
char *b;
} t;
void f1(t s);
void f2(t *p);
main() {
static t s = {"A", "B"};
printf ("%s %s\n", s.a, s.b);
f1(s);
printf ("%s %s\n", s.a, s.b);
f2(&s);
}
void f1(t s) {
s.a = "U"; s.b = "V";
printf ("%s %s\n", s.a, s.b);
return;
}
void f2(t *p) {
p->a = "V"; p->b = "W";
printf("%s %s\n", p->a, p->b);
return;
}What is the output generated by the program?
A. A B U V V W V W
B. A B U V A B V W
C. A B U V U V V W
D. A B U V V W U V
Answer: B. A B U V A B V W.
First, main prints the initial values A B. Next, f1 receives a copy, changes its local fields to U V, and prints them without touching the caller's s. Back in main, the original is still A B. Finally, f2 receives its address, changes the real fields and prints V W, giving A B, U V, A B, V W; pass-by-value copies a structure, while pass-by-pointer can modify the original.
The short version and where to practise more
Structure size is the sum of its members when alignment is ignored. Union size is the size of its largest member because all members share storage. typedef only renames a type, and pass-by-value copies a structure while pass-by-pointer can edit the original.
These ten are a slice of roughly 35 teacher-reviewed C structures and unions questions in the KnowledgeGate practice sets. Continue with the C Language Course, Concepts, MCQs and Coding for full explanations and more solved sets. The wider Coding and Skill Development Courses category places C alongside DSA, while the Data Structures MCQs and Graph MCQs: BFS, DFS, Connectivity collections give you adjacent practice with fresh traps.




