- Defining structures with
structand declaring variables of structure type - Initializing structures at declaration and via member assignment
- Accessing members with the dot (
.) and arrow (->) operators - Computing derived quantities — averages, totals, distances, areas, differences
- Working with nested structures (dates, times) and multi-field records
- Using
typedefto create cleaner structure type names - Passing structures to functions (by value and by reference)
- Building arrays of structures and sorting them with a loop-based sort
- Representing real-world entities (students, books, employees) as composite types
Structures are C's way of grouping related data into a single composite type. Instead of passing around five separate variables for a student (name, roll number, and three subject marks), you define one struct student and treat the whole record as a unit. Every non-trivial C program uses structures — operating systems use them for process tables, databases use them for rows, and games use them for entities. This page collects 25 structure assignments that take you from basic definition and initialization to sorting an array of structures. Each assignment links to a full tutorial with the program code, sample output, and a line-by-line explanation.
🔹 Student Records
Define a student structure, initialize it, and compute derived quantities like averages.
- 1. Create a structure to represent a student with attributes like name, roll number, and marks in three subjects.
- 2. Write a program in C to initialize and display the details of a student using a structure.
- 3. Write a program in C language to find the average marks of a student using a structure.
🔹 Book Records
Model books with title, author, and price — then compute total prices across a collection.
- 4. Create a structure to represent a book with attributes like title, author, and price.
- 5. Write a program in C to initialize and display the details of a book using a structure.
- 6. Write a C program to find the total price of a collection of books using a structure.
🔹 2D Points
Represent coordinates with a point structure and compute distances between them.
- 7. Write a structure using C language to represent a point in 2D space with attributes x and y coordinates.
- 8. Write a C program to initialize and display the coordinates of a point using a structure.
- 9. Write and develop a program in C language to find the distance between two points using a structure.
🔹 Rectangles
Model rectangles with length and width, then compute area and perimeter.
- 10. Create a structure in C language to represent a rectangle with attributes length and width.
- 11. Write a program in C to initialize and display the dimensions of a rectangle using a structure.
- 12. Write a program in C language to calculate the area and perimeter of a rectangle using a structure.
🔹 Dates
Represent dates and compute the difference between them — a classic nested-structure exercise.
- 13. Write a C program to create a structure to represent a date with attributes day, month, and year.
- 14. Write a program in C language to initialize and display the date using a structure.
- 15. Write a program in C to find the difference between two dates using a structure.
🔹 Times
Model times with hours, minutes, and seconds, then add two times together.
- 16. Create a structure using C language to represent a time with attributes hours, minutes, and seconds.
- 17. Write a program in C to initialize and display the time using a structure.
- 18. Write a program in C to add two times using a structure.
🔹 Employees
Model employees with name, ID, and salary — then apply a salary hike.
- 19. Write a C program to create a structure to represent an employee with attributes like name, employee ID, and salary.
- 20. Write a program in C to initialize and display the details of an employee using a structure.
- 21. Write a program in C to give a salary hike to an employee using a structure.
🔹 Complex Numbers
Represent complex numbers with real and imaginary parts — a stepping stone to arithmetic on custom types.
- 22. Write a C program to create a structure to represent a complex number with attributes real and imaginary parts.
- 23. Write a program in C language to initialize and display the complex number using a structure.
🔹 Advanced: Structure Arrays & Sorting
Move beyond single records — define arrays of structures and sort them with loop-based algorithms.
- 24. Write a C program to create a structure to represent a student with attributes like name, age, and address.
- 25. Write a program in C to sort an array of students based on their ages using a structure.
How to Get the Most Out of These Assignments
Reading a solution is not the same as writing one. For each assignment, follow this three-step workflow:
- Define the structure first. Before writing any code, write down the struct definition with its fields and their types. This is the design step — get it right and the rest of the program falls into place.
- Attempt the program. Open a blank editor and try to solve the problem using only the assignment description. Pay attention to how you access members (dot for direct variables, arrow for pointers) and how you pass structures to functions.
- Compare carefully and re-implement. Open the solution page, compare your approach, then close it and write the program again without looking. This is the step most learners skip — and it's the one that actually builds fluency.
💡 Tip: Compile every program with gcc -Wall -Wextra -std=c11 -g. The compiler will warn you about uninitialized struct members, missing & in scanf for struct fields, and type mismatches between float and double — all of which are common when working with structures.
❓ Structure Viva Questions & Quick Answers
Q1. What is a structure in C, and why use it?
A structure is a user-defined composite data type that groups variables of different types under one name. Use it when you have a logical entity (a student, a book, a point) that has multiple attributes — instead of passing five separate variables around, you pass one struct.
Q2. What's the difference between . and -> when accessing structure members?
Use . when you have a structure variable directly: s.name. Use -> when you have a pointer to a structure: p->name, which is shorthand for (*p).name. Mixing them up is the most common beginner error.
Q3. Can I compare two structures with ==?
No. C doesn't provide built-in structure comparison. To check whether two structures are equal, you must compare their members field-by-field. (You can assign one structure to another with =, which copies all bytes — that's allowed.)
Q4. What is typedef struct and why is it used?
typedef creates an alias for the structure type, so you can write Student s; instead of struct student s;. It's a convenience, not a necessity, but it makes code cleaner — especially in function signatures. Example: typedef struct { char name[50]; int age; } Student;
Q5. How do I pass a structure to a function — by value or by reference?
Both work. Passing by value copies the entire structure (costly for large structs); passing by reference (pointer) avoids the copy and allows the function to modify the original. For read-only access, pass a const pointer: void display(const Student *s). This is the idiomatic C approach.
Q6. How does memory layout of a structure work — is it contiguous?
Yes, members are laid out in declaration order, and the total size is at least the sum of the members — but the compiler may add padding bytes between members for alignment. This is why sizeof(struct S) can be larger than the sum of sizeof of its members. Use sizeof rather than manual addition to compute structure size.