Practical / Solution
Nested Structures — Exercise 1
Problem
Write a C program to create a structure Address containing
city and PIN code, and include it inside a Student structure.
Display all student details.
Program
#include <stdio.h>
struct Address
{
char city[50];
int pin;
};
struct Student
{
int roll;
char name[50];
struct Address address;
};
int main()
{
struct Student s;
printf("Enter roll number: ");
scanf("%d", &s.roll);
printf("Enter name: ");
scanf("%49s", s.name);
printf("Enter city: ");
scanf("%49s", s.address.city);
printf("Enter PIN code: ");
scanf("%d", &s.address.pin);
// Access nested structure members
printf("\nRoll = %d\n", s.roll);
printf("Name = %s\n", s.name);
printf("City = %s\n", s.address.city);
printf("PIN = %d\n", s.address.pin);
return 0;
}
Explanation
A nested structure is a structure used as a member of another structure.
Here, Address is nested inside Student.
Expected Output
Enter roll number: 101
Enter name: Rahul
Enter city: Jaipur
Enter PIN code: 302001
Roll = 101
Name = Rahul
City = Jaipur
PIN = 302001
Nested Structures — Exercise 2
Problem
Write a C program to store a student's date of birth using a nested
Date structure inside a Student structure.
Program
#include <stdio.h>
struct Date
{
int day;
int month;
int year;
};
struct Student
{
int roll;
char name[50];
struct Date dob;
};
int main()
{
struct Student s;
printf("Enter roll number: ");
scanf("%d", &s.roll);
printf("Enter name: ");
scanf("%49s", s.name);
printf("Enter date of birth (day month year): ");
scanf("%d %d %d",
&s.dob.day,
&s.dob.month,
&s.dob.year);
// Display nested date members
printf("\nRoll = %d\n", s.roll);
printf("Name = %s\n", s.name);
printf("Date of Birth = %02d/%02d/%d\n",
s.dob.day,
s.dob.month,
s.dob.year);
return 0;
}
Explanation
The Date structure becomes a member of Student.
Its members are accessed using multiple dot operators such as
s.dob.year.
Expected Output
Enter roll number: 101
Enter name: Priya
Enter date of birth (day month year): 15 8 2005
Roll = 101
Name = Priya
Date of Birth = 15/08/2005
Nested Structures — Exercise 3
Problem
Write a C program to store employee details with a nested
Address structure containing city and state.
Program
#include <stdio.h>
struct Address
{
char city[50];
char state[50];
};
struct Employee
{
int id;
char name[50];
float salary;
struct Address address;
};
int main()
{
struct Employee emp;
printf("Enter employee ID: ");
scanf("%d", &emp.id);
printf("Enter name: ");
scanf("%49s", emp.name);
printf("Enter salary: ");
scanf("%f", &emp.salary);
printf("Enter city: ");
scanf("%49s", emp.address.city);
printf("Enter state: ");
scanf("%49s", emp.address.state);
// Display employee record
printf("\nEmployee ID = %d\n", emp.id);
printf("Name = %s\n", emp.name);
printf("Salary = %.2f\n", emp.salary);
printf("City = %s\n", emp.address.city);
printf("State = %s\n", emp.address.state);
return 0;
}
Explanation
Nested structures help group sub-records within a larger record.
Here, employee information contains a separate address record.
Expected Output
Enter employee ID: 501
Enter name: Amit
Enter salary: 45000
Enter city: Jaipur
Enter state: Rajasthan
Employee ID = 501
Name = Amit
Salary = 45000.00
City = Jaipur
State = Rajasthan
Nested Structures — Exercise 4
Problem
Write a C program to create a Student structure containing
a nested Marks structure for three subjects. Calculate
the total and average marks.
Program
#include <stdio.h>
struct Marks
{
int math;
int cProgramming;
int computer;
};
struct Student
{
int roll;
char name[50];
struct Marks marks;
};
int main()
{
struct Student s;
int total;
float average;
printf("Enter roll number: ");
scanf("%d", &s.roll);
printf("Enter name: ");
scanf("%49s", s.name);
printf("Enter marks in three subjects: ");
scanf("%d %d %d",
&s.marks.math,
&s.marks.cProgramming,
&s.marks.computer);
// Calculate total using nested members
total = s.marks.math +
s.marks.cProgramming +
s.marks.computer;
average = total / 3.0;
printf("Total = %d\n", total);
printf("Average = %.2f\n", average);
return 0;
}
Explanation
The nested Marks structure keeps subject marks together.
Members are accessed through expressions such as s.marks.math.
Expected Output
Enter roll number: 101
Enter name: Rahul
Enter marks in three subjects: 80 90 85
Total = 255
Average = 85.00
Nested Structures — Exercise 5
Problem
Write a C program to store product information along with a nested
ManufactureDate structure and display the complete record.
Program
#include <stdio.h>
struct Date
{
int day;
int month;
int year;
};
struct Product
{
int id;
char name[50];
float price;
struct Date manufactureDate;
};
int main()
{
struct Product p;
printf("Enter product ID: ");
scanf("%d", &p.id);
printf("Enter product name: ");
scanf("%49s", p.name);
printf("Enter price: ");
scanf("%f", &p.price);
printf("Enter manufacture date: ");
scanf("%d %d %d",
&p.manufactureDate.day,
&p.manufactureDate.month,
&p.manufactureDate.year);
// Display product details
printf("\nProduct ID = %d\n", p.id);
printf("Name = %s\n", p.name);
printf("Price = %.2f\n", p.price);
printf("Manufacture Date = %02d/%02d/%d\n",
p.manufactureDate.day,
p.manufactureDate.month,
p.manufactureDate.year);
return 0;
}
Explanation
A nested structure is useful when one record contains another logical
record. Here, the product contains a separate date record.
Expected Output
Enter product ID: 201
Enter product name: Keyboard
Enter price: 800
Enter manufacture date: 10 6 2026
Product ID = 201
Name = Keyboard
Price = 800.00
Manufacture Date = 10/06/2026
Nested Structures — Exercise 6
Problem
Write a C program to compare the marks of two students when the
marks are stored inside a nested structure.
Program
#include <stdio.h>
struct Marks
{
float marks;
};
struct Student
{
int roll;
char name[50];
struct Marks result;
};
int main()
{
struct Student s1, s2;
printf("Enter first student roll, name and marks: ");
scanf("%d %49s %f",
&s1.roll,
s1.name,
&s1.result.marks);
printf("Enter second student roll, name and marks: ");
scanf("%d %49s %f",
&s2.roll,
s2.name,
&s2.result.marks);
// Compare nested marks members
if (s1.result.marks > s2.result.marks)
printf("%s has higher marks.\n", s1.name);
else if (s2.result.marks > s1.result.marks)
printf("%s has higher marks.\n", s2.name);
else
printf("Both students have equal marks.\n");
return 0;
}
Explanation
Nested members can be used in expressions and conditions just like
ordinary structure members. The complete access path is
s1.result.marks.
Expected Output
Enter first student roll, name and marks: 101 Rahul 75
Enter second student roll, name and marks: 102 Priya 88
Priya has higher marks.
Nested Structures — Exercise 7
Problem
Write a C program to store an employee's joining date using a nested
date structure and calculate the number of years since joining based
on the current year.
Program
#include <stdio.h>
struct Date
{
int day;
int month;
int year;
};
struct Employee
{
int id;
char name[50];
struct Date joiningDate;
};
int main()
{
struct Employee emp;
int currentYear, years;
printf("Enter employee ID: ");
scanf("%d", &emp.id);
printf("Enter name: ");
scanf("%49s", emp.name);
printf("Enter joining date (day month year): ");
scanf("%d %d %d",
&emp.joiningDate.day,
&emp.joiningDate.month,
&emp.joiningDate.year);
printf("Enter current year: ");
scanf("%d", ¤tYear);
// Calculate approximate years of service
years = currentYear - emp.joiningDate.year;
printf("\nEmployee = %s\n", emp.name);
printf("Joining Date = %02d/%02d/%d\n",
emp.joiningDate.day,
emp.joiningDate.month,
emp.joiningDate.year);
printf("Years of Service = %d\n", years);
return 0;
}
Explanation
A nested date structure allows date information to remain grouped
inside the employee record. Its members can be accessed using
multiple dot operators.
Expected Output
Enter employee ID: 501
Enter name: Amit
Enter joining date (day month year): 10 7 2021
Enter current year: 2026
Employee = Amit
Joining Date = 10/07/2021
Years of Service = 5
Nested Structures — Exercise 8
Problem
Write a C program to create an Address structure and
include it inside a Customer structure. Modify the
customer's city after input and display the updated record.
Program
#include <stdio.h>
struct Address
{
char city[50];
char state[50];
};
struct Customer
{
int id;
char name[50];
struct Address address;
};
int main()
{
struct Customer c;
printf("Enter customer ID: ");
scanf("%d", &c.id);
printf("Enter name: ");
scanf("%49s", c.name);
printf("Enter city: ");
scanf("%49s", c.address.city);
printf("Enter state: ");
scanf("%49s", c.address.state);
// Modify nested structure member
printf("Enter updated city: ");
scanf("%49s", c.address.city);
printf("\nCustomer ID = %d\n", c.id);
printf("Name = %s\n", c.name);
printf("City = %s\n", c.address.city);
printf("State = %s\n", c.address.state);
return 0;
}
Explanation
Nested structure members can also be modified directly. The expression
c.address.city accesses the city member inside the
nested address structure.
Expected Output
Enter customer ID: 1001
Enter name: Ravi
Enter city: Delhi
Enter state: Delhi
Enter updated city: Jaipur
Customer ID = 1001
Name = Ravi
City = Jaipur
State = Delhi
Nested Structures — Exercise 9
Problem
Write a C program to pass a structure containing a nested structure
to a function and display its details.
Program
#include <stdio.h>
struct Address
{
char city[50];
int pin;
};
struct Student
{
int roll;
char name[50];
struct Address address;
};
void displayStudent(struct Student s)
{
// Access nested structure members
printf("Roll = %d\n", s.roll);
printf("Name = %s\n", s.name);
printf("City = %s\n", s.address.city);
printf("PIN = %d\n", s.address.pin);
}
int main()
{
struct Student s;
printf("Enter roll number: ");
scanf("%d", &s.roll);
printf("Enter name: ");
scanf("%49s", s.name);
printf("Enter city: ");
scanf("%49s", s.address.city);
printf("Enter PIN code: ");
scanf("%d", &s.address.pin);
// Pass complete structure to function
displayStudent(s);
return 0;
}
Explanation
A structure containing another structure can be passed to a function
just like other structure variables. The function can access nested
members using the complete member path.
Expected Output
Enter roll number: 101
Enter name: Neha
Enter city: Jaipur
Enter PIN code: 302001
Roll = 101
Name = Neha
City = Jaipur
PIN = 302001
Nested Structures — Exercise 10
Problem
Write a C program to create a complete employee record using nested
structures for address and joining date. Display the formatted record.
Program
#include <stdio.h>
struct Address
{
char city[50];
char state[50];
};
struct Date
{
int day;
int month;
int year;
};
struct Employee
{
int id;
char name[50];
float salary;
struct Address address;
struct Date joiningDate;
};
int main()
{
struct Employee emp;
printf("Enter employee ID: ");
scanf("%d", &emp.id);
printf("Enter name: ");
scanf("%49s", emp.name);
printf("Enter salary: ");
scanf("%f", &emp.salary);
printf("Enter city and state: ");
scanf("%49s %49s",
emp.address.city,
emp.address.state);
printf("Enter joining date (day month year): ");
scanf("%d %d %d",
&emp.joiningDate.day,
&emp.joiningDate.month,
&emp.joiningDate.year);
// Display complete nested record
printf("\n-----------------------------\n");
printf(" EMPLOYEE RECORD\n");
printf("-----------------------------\n");
printf("ID : %d\n", emp.id);
printf("Name : %s\n", emp.name);
printf("Salary : %.2f\n", emp.salary);
printf("City : %s\n", emp.address.city);
printf("State : %s\n", emp.address.state);
printf("Joining Date : %02d/%02d/%d\n",
emp.joiningDate.day,
emp.joiningDate.month,
emp.joiningDate.year);
printf("-----------------------------\n");
return 0;
}
Explanation
This example combines multiple nested structures inside one main
structure. The employee record contains separate address and date
records, demonstrating how complex information can be organized
hierarchically.
Expected Output
Enter employee ID: 501
Enter name: Amit
Enter salary: 45000
Enter city and state: Jaipur Rajasthan
Enter joining date (day month year): 10 7 2021
-----------------------------
EMPLOYEE RECORD
-----------------------------
ID : 501
Name : Amit
Salary : 45000.00
City : Jaipur
State : Rajasthan
Joining Date : 10/07/2021
-----------------------------