Structures and unions are user-defined data types in C that allow you to group multiple variables of different data types under a single name.
- Structure (
struct) allocates separate memory for each member. - Union (
union) shares the same memory among all members.
Structures are widely used in employee management systems, student databases, banking software, hospital management systems, inventory applications, and many real-world projects. Unions are commonly used in embedded systems, memory optimization, hardware programming, and low-level software development.
In this chapter, you’ll solve practical programming questions on structures and unions with complete solutions, sample outputs, explanations, and concepts covered. C Structures and Unions Practice Questions with Solutions help to understand the concepts.
1. C Program to Store and Display Student Information Using Structure
Problem Statement
Write a C program to store and display student details using a structure.
C Solution
#include <stdio.h>
struct Student
{
int rollNumber;
char name[50];
float marks;
};
int main()
{
struct Student student;
printf("Enter Roll Number: ");
scanf("%d", &student.rollNumber);
printf("Enter Name: ");
scanf("%s", student.name);
printf("Enter Marks: ");
scanf("%f", &student.marks);
printf("\nStudent Details\n");
printf("Roll Number : %d\n", student.rollNumber);
printf("Name : %s\n", student.name);
printf("Marks : %.2f\n", student.marks);
return 0;
}
Sample Output
Enter Roll Number: 101
Enter Name: Rahul
Enter Marks: 89.5
Student Details
Roll Number : 101
Name : Rahul
Marks : 89.50
Explanation
The structure groups different data types (int, char, and float) into a single user-defined data type named Student.
Concepts Covered
- Structures
- User-defined Data Types
- Structure Variables
- Input and Output
2. C Program to Store Employee Information Using Structure
Problem Statement
Write a C program to store employee details using a structure.
C Solution
#include <stdio.h>
struct Employee
{
int employeeID;
char name[50];
float salary;
};
int main()
{
struct Employee employee;
printf("Enter Employee ID: ");
scanf("%d", &employee.employeeID);
printf("Enter Employee Name: ");
scanf("%s", employee.name);
printf("Enter Salary: ");
scanf("%f", &employee.salary);
printf("\nEmployee Details\n");
printf("Employee ID : %d\n", employee.employeeID);
printf("Name : %s\n", employee.name);
printf("Salary : %.2f\n", employee.salary);
return 0;
}
Sample Output
Enter Employee ID: 2001
Enter Employee Name: Aman
Enter Salary: 55000
Employee Details
Employee ID : 2001
Name : Aman
Salary : 55000.00
Explanation
The program stores all employee-related information inside a single structure variable.
Concepts Covered
- Structures
- Employee Records
- Structure Members
- User-defined Data Types
3. C Program to Find the Size of a Structure
Problem Statement
Write a C program to determine the size of a structure using the sizeof() operator.
C Solution
#include <stdio.h>
struct Student
{
int rollNumber;
char grade;
float marks;
};
int main()
{
printf("Size of Structure = %lu bytes",
sizeof(struct Student));
return 0;
}
Sample Output
Size of Structure = 12 bytes
Note: The size may vary depending on the compiler, operating system, and memory alignment rules.
Explanation
The sizeof() operator returns the total memory occupied by the structure, including any padding added by the compiler.
Concepts Covered
- Structures
- sizeof()
- Memory Allocation
- Structure Padding
4. C Program to Initialize a Structure
Problem Statement
Write a C program to initialize a structure while declaring it and display its values.
C Solution
#include <stdio.h>
struct Student
{
int rollNumber;
char name[50];
float marks;
};
int main()
{
struct Student student = {101, "Rahul", 92.50};
printf("Student Details\n");
printf("Roll Number : %d\n", student.rollNumber);
printf("Name : %s\n", student.name);
printf("Marks : %.2f\n", student.marks);
return 0;
}
Sample Output
Student Details
Roll Number : 101
Name : Rahul
Marks : 92.50
Explanation
Instead of taking input from the user, the structure members are initialized directly during declaration.
Concepts Covered
- Structure Initialization
- User-defined Data Types
- Structure Members
- Output Formatting
5. C Program to Store Multiple Student Records Using Structure Array
Problem Statement
Write a C program to store and display the information of multiple students using an array of structures.
C Solution
#include <stdio.h>
struct Student
{
int rollNumber;
char name[50];
float marks;
};
int main()
{
struct Student student[3];
int i;
for(i = 0; i < 3; i++)
{
printf("\nEnter Details of Student %d\n", i + 1);
printf("Roll Number: ");
scanf("%d", &student[i].rollNumber);
printf("Name: ");
scanf("%s", student[i].name);
printf("Marks: ");
scanf("%f", &student[i].marks);
}
printf("\nStudent Records\n");
for(i = 0; i < 3; i++)
{
printf("\nStudent %d\n", i + 1);
printf("Roll Number : %d\n", student[i].rollNumber);
printf("Name : %s\n", student[i].name);
printf("Marks : %.2f\n", student[i].marks);
}
return 0;
}
Sample Output
Enter Details of Student 1
Roll Number: 101
Name: Rahul
Marks: 89
Enter Details of Student 2
Roll Number: 102
Name: Amit
Marks: 92
Enter Details of Student 3
Roll Number: 103
Name: Priya
Marks: 95
Student Records
Student 1
Roll Number : 101
Name : Rahul
Marks : 89.00
Student 2
Roll Number : 102
Name : Amit
Marks : 92.00
Student 3
Roll Number : 103
Name : Priya
Marks : 95.00
Explanation
An array of structures allows multiple records of the same type to be stored efficiently. Each array element represents one student record.
Concepts Covered
- Arrays of Structures
- Multiple Records
- Loops
- Structure Members
6. C Program to Pass a Structure to a Function
Problem Statement
Write a C program to pass a structure as an argument to a function and display its details.
C Solution
#include <stdio.h>
struct Student
{
int rollNumber;
char name[50];
float marks;
};
void display(struct Student student)
{
printf("\nStudent Details\n");
printf("Roll Number : %d\n", student.rollNumber);
printf("Name : %s\n", student.name);
printf("Marks : %.2f\n", student.marks);
}
int main()
{
struct Student student;
printf("Enter Roll Number: ");
scanf("%d", &student.rollNumber);
printf("Enter Name: ");
scanf("%s", student.name);
printf("Enter Marks: ");
scanf("%f", &student.marks);
display(student);
return 0;
}
Sample Output
Enter Roll Number: 101
Enter Name: Rahul
Enter Marks: 90
Student Details
Roll Number : 101
Name : Rahul
Marks : 90.00
Explanation
The entire structure is passed to the function as an argument. The function receives a copy of the structure and displays its contents.
Concepts Covered
- Structures
- Function Arguments
- Structure Passing
- User-defined Data Types
7. C Program to Return a Structure from a Function
Problem Statement
Write a C program to return a structure from a function.
C Solution
#include <stdio.h>
struct Student
{
int rollNumber;
char name[50];
float marks;
};
struct Student getData()
{
struct Student student;
printf("Enter Roll Number: ");
scanf("%d", &student.rollNumber);
printf("Enter Name: ");
scanf("%s", student.name);
printf("Enter Marks: ");
scanf("%f", &student.marks);
return student;
}
int main()
{
struct Student student;
student = getData();
printf("\nStudent Details\n");
printf("Roll Number : %d\n", student.rollNumber);
printf("Name : %s\n", student.name);
printf("Marks : %.2f\n", student.marks);
return 0;
}
Sample Output
Enter Roll Number: 105
Enter Name: Neha
Enter Marks: 91
Student Details
Roll Number : 105
Name : Neha
Marks : 91.00
Explanation
The function creates a structure variable, fills it with user input, and returns the complete structure to the calling function.
Concepts Covered
- Structures
- Returning Structures
- Functions
- User-defined Data Types
8. C Program to Demonstrate Nested Structures
Problem Statement
Write a C program to create and use a nested structure.
C Solution
#include <stdio.h>
struct Address
{
char city[30];
char state[30];
};
struct Student
{
int rollNumber;
char name[50];
struct Address address;
};
int main()
{
struct Student student;
printf("Enter Roll Number: ");
scanf("%d", &student.rollNumber);
printf("Enter Name: ");
scanf("%s", student.name);
printf("Enter City: ");
scanf("%s", student.address.city);
printf("Enter State: ");
scanf("%s", student.address.state);
printf("\nStudent Details\n");
printf("Roll Number : %d\n", student.rollNumber);
printf("Name : %s\n", student.name);
printf("City : %s\n", student.address.city);
printf("State : %s\n", student.address.state);
return 0;
}
Sample Output
Enter Roll Number: 101
Enter Name: Rahul
Enter City: Delhi
Enter State: Delhi
Student Details
Roll Number : 101
Name : Rahul
City : Delhi
State : Delhi
Explanation
A structure (Address) is declared inside another structure (Student). This is known as a nested structure.
Concepts Covered
- Nested Structures
- Structures
- User-defined Data Types
- Member Access
9. C Program to Demonstrate Structure Pointer
Problem Statement
Write a C program to access structure members using a pointer.
C Solution
#include <stdio.h>
struct Student
{
int rollNumber;
char name[50];
float marks;
};
int main()
{
struct Student student;
struct Student *ptr;
ptr = &student;
printf("Enter Roll Number: ");
scanf("%d", &ptr->rollNumber);
printf("Enter Name: ");
scanf("%s", ptr->name);
printf("Enter Marks: ");
scanf("%f", &ptr->marks);
printf("\nStudent Details\n");
printf("Roll Number : %d\n", ptr->rollNumber);
printf("Name : %s\n", ptr->name);
printf("Marks : %.2f\n", ptr->marks);
return 0;
}
Sample Output
Enter Roll Number: 110
Enter Name: Priya
Enter Marks: 96
Student Details
Roll Number : 110
Name : Priya
Marks : 96.00
Explanation
A structure pointer stores the address of a structure variable. The arrow operator (->) is used to access the structure members.
Concepts Covered
- Structure Pointer
- Pointer to Structure
- Arrow Operator
- Memory Address
10. C Program to Access Structure Members Using the Arrow (->) Operator
Problem Statement
Write a C program to demonstrate the use of the arrow (->) operator with a structure pointer.
C Solution
#include <stdio.h>
struct Employee
{
int employeeID;
char name[50];
};
int main()
{
struct Employee employee = {201, "Aman"};
struct Employee *ptr;
ptr = &employee;
printf("Employee ID : %d\n", ptr->employeeID);
printf("Employee Name : %s\n", ptr->name);
return 0;
}
Sample Output
Employee ID : 201
Employee Name : Aman
Explanation
The arrow operator (->) provides a convenient way to access structure members through a pointer without explicitly dereferencing it.
Example:
ptr->employeeID
is equivalent to:
(*ptr).employeeID
Concepts Covered
- Structure Pointer
- Arrow Operator
- Structures
- Pointer Access
11. C Program to Demonstrate Union
Problem Statement
Write a C program to demonstrate how a union stores different types of data.
C Solution
#include <stdio.h>
union Data
{
int integerValue;
float floatValue;
char character;
};
int main()
{
union Data data;
data.integerValue = 100;
printf("Integer Value = %d\n", data.integerValue);
data.floatValue = 45.75;
printf("Float Value = %.2f\n", data.floatValue);
data.character = 'A';
printf("Character = %c\n", data.character);
return 0;
}
Sample Output
Integer Value = 100
Float Value = 45.75
Character = A
Explanation
Unlike structures, all members of a union share the same memory location. Assigning a new value overwrites the previously stored value.
Concepts Covered
- Union
- Shared Memory
- User-defined Data Types
- Memory Management
12. C Program to Compare Structure and Union Memory
Problem Statement
Write a C program to compare the memory occupied by a structure and a union.
C Solution
#include <stdio.h>
struct Student
{
int rollNumber;
float marks;
char grade;
};
union StudentData
{
int rollNumber;
float marks;
char grade;
};
int main()
{
printf("Size of Structure = %lu bytes\n",
sizeof(struct Student));
printf("Size of Union = %lu bytes",
sizeof(union StudentData));
return 0;
}
Sample Output
Size of Structure = 12 bytes
Size of Union = 4 bytes
Note: The exact size may vary depending on the compiler and system architecture.
Explanation
A structure allocates separate memory for each member, while a union allocates memory equal to its largest member.
Concepts Covered
- Structure
- Union
- Memory Allocation
- sizeof()
13. C Program to Store Student Information Using Union
Problem Statement
Write a C program to store and display student information using a union.
C Solution
#include <stdio.h>
union Student
{
int rollNumber;
float marks;
};
int main()
{
union Student student;
student.rollNumber = 101;
printf("Roll Number = %d\n", student.rollNumber);
student.marks = 89.50;
printf("Marks = %.2f\n", student.marks);
return 0;
}
Sample Output
Roll Number = 101
Marks = 89.50
Explanation
Since all members share the same memory location, storing a new value replaces the previous one.
Concepts Covered
- Union
- Shared Memory
- Memory Overwriting
- User-defined Data Types
14. C Program to Calculate the Size of a Union
Problem Statement
Write a C program to determine the size of a union using the sizeof() operator.
C Solution
#include <stdio.h>
union Data
{
int integerValue;
float floatValue;
char character;
};
int main()
{
printf("Size of Union = %lu bytes",
sizeof(union Data));
return 0;
}
Sample Output
Size of Union = 4 bytes
Explanation
The size of a union is determined by the size of its largest member.
Concepts Covered
- Union
- sizeof()
- Memory Allocation
- Largest Member Rule
15. C Program to Demonstrate the Difference Between Structure and Union
Problem Statement
Write a C program to demonstrate the difference between a structure and a union.
C Solution
#include <stdio.h>
struct Example
{
int integerValue;
float floatValue;
};
union Demo
{
int integerValue;
float floatValue;
};
int main()
{
struct Example structureData;
union Demo unionData;
structureData.integerValue = 50;
structureData.floatValue = 75.25;
unionData.integerValue = 50;
unionData.floatValue = 75.25;
printf("Structure Values\n");
printf("Integer = %d\n", structureData.integerValue);
printf("Float = %.2f\n\n", structureData.floatValue);
printf("Union Values\n");
printf("Float = %.2f\n", unionData.floatValue);
return 0;
}
Sample Output
Structure Values
Integer = 50
Float = 75.25
Union Values
Float = 75.25
Explanation
In a structure, each member has its own memory.
In a union, all members share the same memory location, so only the most recently assigned value is retained.
Concepts Covered
- Structure
- Union
- Memory Sharing
- Memory Allocation
Chapter Summary
In this chapter, you learned how structures and unions help organize related data in C programming. You practiced creating structures, initializing them, passing and returning structures from functions, using arrays of structures, nested structures, structure pointers, and the arrow (->) operator. You also explored unions, memory sharing, memory optimization, and the key differences between structures and unions. These concepts are widely used in real-world software, embedded systems, operating systems, and database applications.
Key Takeaways
- Structures group multiple variables of different data types under one name.
- Each structure member has its own memory location.
- Arrays of structures store multiple records efficiently.
- Structures can be passed to and returned from functions.
- Nested structures help organize complex data.
- Structure pointers improve memory access and performance.
- The arrow (
->) operator is used to access members through a structure pointer. - Unions allow multiple members to share the same memory location.
- A union occupies memory equal to its largest member.
- Structures and unions are fundamental concepts for advanced C programming and system-level development.
Frequently Asked Questions (FAQs)
1. What is a structure in C?
A structure is a user-defined data type that groups variables of different data types under a single name.
2. What is a union in C?
A union is a user-defined data type where all members share the same memory location.
3. What is the main difference between a structure and a union?
A structure allocates separate memory for each member, while a union shares one memory location among all members.
4. When should a structure be used?
Structures are used when all data members need to be stored simultaneously, such as employee records, student databases, banking systems, and inventory management.
5. When should a union be used?
Unions are used when memory optimization is important and only one member is needed at a time, especially in embedded systems and hardware programming.
6. What is the arrow (->) operator?
The arrow operator is used to access structure members through a pointer to a structure.
7. Can a structure contain another structure?
Yes. This is called a nested structure, where one structure is declared as a member of another structure.
8. Where are structures and unions used in real-world programming?
Structures and unions are widely used in operating systems, embedded systems, networking, database management systems, compilers, file systems, graphics programming, device drivers, and many other real-world software applications.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
