Structure Assignments in C — 25 Programs with Solutions
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

Structure Assignments in C Language with Explanation and Solutions

📑 Jump to a category:
  • Student Records (1–3)
  • Book Records (4–6)
  • 2D Points (7–9)
  • Rectangles (10–12)
  • Dates (13–15)
  • Times (16–18)
  • Employees (19–21)
  • Complex Numbers (22–23)
  • Advanced Student & Sorting (24–25)
📚 What you'll practice across these 25 assignments:
  • Defining structures with struct and 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 typedef to 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.

Structure assignments in C language with step-by-step explanation, solution and description — student, book, point, rectangle, date, time, employee, and complex number structures.

🔹 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:

  1. 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.
  2. 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.
  3. 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.

📖 Related Tutorials

  • Introduction to Structures in C
  • Defining a Structure
  • Declaring Structure Variables
  • Accessing Structure Members
  • Structure Initialization
  • Array of Structures
  • Structures Within Structures
  • Structures and Functions
  • Structures FAQ
  • Structures Multiple-Choice Questions

Previous Topic: -->> Function Assignments in C   ||   Next Topic: -->> Pointer Assignments in C


C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • CONDITIONAL STATEMENTS
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • LOOPS STATEMENTS
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processiong
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.