Structures and unions are essential user-defined data types in C, widely used in system-level programming, embedded development, memory-constrained applications, and low-level data manipulation. These questions test how well you understand memory layout, data handling, padding, alignment, and practical implementation scenarios.
1. What is a structure in C? Give a basic example.
A structure is a user-defined datatype that groups variables of different datatypes under a single name.
#include <stdio.h>
struct Employee {
int id;
float salary;
char grade;
};
int main() {
struct Employee e1 = {101, 55000.5, 'A'};
printf("ID: %d, Salary: %.1f, Grade: %c\n", e1.id, e1.salary, e1.grade);
return 0;
}
2. What is a union in C and how does it differ from a structure?
A union allows storing different datatypes in the same memory location. Only one value can be stored at a time, unlike structures where each member has its own memory.
union Data {
int i;
float f;
char ch;
};
3. How is memory allocated for a structure vs. a union?
- Structure: memory = sum of all members (plus padding).
- Union: memory = size of the largest member.
Example:
struct S { int x; double y; }; // 4 + 8 = 12 (may round to 16 due to alignment)
union U { int x; double y; }; // 8
4. Write a C program to input and print details of N students using structures.
This tests structure arrays and user input handling.
#include <stdio.h>
struct Student {
char name[20];
int age;
};
int main() {
int n = 2;
struct Student s[2];
for (int i = 0; i < n; i++) {
scanf("%s %d", s[i].name, &s[i].age);
}
for (int i = 0; i < n; i++) {
printf("%s %d\n", s[i].name, s[i].age);
}
}
Input:
Elon 42
Hisenberg 45
Output:
Elon 42
Hisenberg 45
5. Can we assign one structure variable to another?
Yes, C allows direct assignment because structures support copy semantics.
struct Point { int x, y; };
struct Point p1 = {10, 20};
struct Point p2 = p1; // valid
6. Why can't you directly compare two structure variables using == ?
Because C does not perform byte-wise comparison of structure objects. You must compare field by field.
7. What is structure padding and why is it used?
Padding is added by the compiler so members align correctly in memory for efficient CPU access.
Example:
struct A {
char c; // 1 byte
int x; // 4 bytes, but shifted to 4-byte boundary
};
8. How do you remove structure padding?
Use #pragma pack(1) or compiler-specific attributes.
#pragma pack(1)
struct Data {
char c;
int x;
};
9. Can a structure contain another structure? Write an example.
Yes, called nested structures.
struct Date { int d, m, y; };
struct Employee {
char name[20];
struct Date doj;
};
10. What happens when you write into one member of a union and read another?
You get implementation-dependent garbage, except when using type punning with proper alignment rules.
Example:
union Test { int i; float f; };
Writing to i and reading f invokes type punning.
11. Write a C program to demonstrate how writing to one union member overwrites the others.
#include <stdio.h>
union Demo {
int i;
char c;
};
int main() {
union Demo d;
d.i = 300;
printf("%d\n", d.i);
d.c = 'A';
printf("%d\n", d.i); // Overwritten by 'A'
}
12. What are bit-fields inside structures? Provide an example.
Bit-fields allow packing data in memory with specific bit widths-useful in embedded systems.
struct Flags {
unsigned int a : 1;
unsigned int b : 2;
unsigned int c : 5;
};
13. Difference between structure pointer (->) and dot operator (.)?
. is used with structure variables.
-> is used with structure pointers.
struct A { int x; };
struct A s = {10};
struct A *p = &s;printf("%d %d", s.x, p->x);
14. Write a program to dynamically allocate memory for a structure using malloc().
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
int main() {
struct Node *ptr = malloc(sizeof(struct Node));
ptr->data = 50;
ptr->next = NULL;
printf("%d\n", ptr->data);
free(ptr);
}
15. How do unions help in implementing variant data structures? Demonstrate with code.
Variant types store data that may change type based on a tag (like in protocol parsers).
#include <stdio.h>
union Value {
int i;
float f;
};
struct Variant {
int tag; // 1 = int, 2 = float
union Value v;
};
int main() {
struct Variant var;
var.tag = 1;
var.v.i = 42;
if (var.tag == 1)
printf("INT: %d\n", var.v.i);
var.tag = 2;
var.v.f = 3.14;
if (var.tag == 2)
printf("FLOAT: %.2f\n", var.v.f);
}