This section lists the programs covered under Structures and Their Applications in C. Click on a program title to jump directly to its detailed explanation.
Write a C program to create a Student structure containing name, roll number and grade as structure members. Display the name, roll number and grade of a student.
Student with members: name, rollNo and grade.s of type Student.s.
#include <stdio.h>
struct Student {
char name[50];
int rollNo;
char grade;
};
int main() {
struct Student s;
printf("Enter student name:\n");
scanf("%49s", s.name);
printf("Enter roll number:\n");
scanf("%d", &s.rollNo);
printf("Enter grade:\n");
scanf(" %c", &s.grade);
printf("\nStudent Details:\n");
printf("Name: %s\n", s.name);
printf("Roll Number: %d\n", s.rollNo);
printf("Grade: %c\n", s.grade);
return 0;
}
Enter student name:
Ravi
Enter roll number:
101
Enter grade:
A
Student Details:
Name: Ravi
Roll Number: 101
Grade: A
The structure Student groups related data items (name, roll number and grade) under a single user-defined type, which makes it easier to treat the whole record as one unit. The variable s of type struct Student holds the details for a single student.
The program reads values into the members of s using the dot operator (for example, s.rollNo) and then prints them. This illustrates how structures can be used to model real-world entities such as student records in a clear and organized way.
Write a C program to create a Student structure containing name, roll number and grade as structure members. Display the details of n students using array of structures.
Student with members: name, rollNo and grade.n.s of Student structures of size at least n.n, read and store name, roll number and grade in s[i].
#include <stdio.h>
struct Student {
char name[50];
int rollNo;
char grade;
};
int main() {
struct Student s[50];
int n, i;
printf("Enter number of students:\n");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("\nEnter details of student %d\n", i + 1);
printf("Name:\n");
scanf("%49s", s[i].name);
printf("Roll Number:\n");
scanf("%d", &s[i].rollNo);
printf("Grade:\n");
scanf(" %c", &s[i].grade);
}
printf("\nStudent Details:\n");
for (i = 0; i < n; i++) {
printf("\nStudent %d\n", i + 1);
printf("Name: %s\n", s[i].name);
printf("Roll Number: %d\n", s[i].rollNo);
printf("Grade: %c\n", s[i].grade);
}
return 0;
}
Enter number of students:
2
Enter details of student 1
Name:
Ravi
Roll Number:
101
Grade:
A
Enter details of student 2
Name:
Sita
Roll Number:
102
Grade:
B
Student Details:
Student 1
Name: Ravi
Roll Number: 101
Grade: A
Student 2
Name: Sita
Roll Number: 102
Grade: B
An array of structures s[50] lets the program store up to 50 separate student records, each with its own name, roll number and grade. Each element s[i] behaves like an individual Student variable with its own set of members.
Looping over the array makes it easy to input and output all student records in sequence. This pattern is common in applications such as student databases, where many similar records must be stored and processed together in a uniform way.
Write a C program to add two complex numbers by passing a structure to a function.
Complex with members real and imag for the real and imaginary parts.c1 and c2 of type Complex and read their values from the user.addComplex() that takes two Complex arguments and returns their sum as a Complex value.addComplex(c1, c2) from main() and store the result in sum.a + bi.
#include <stdio.h>
struct Complex {
float real;
float imag;
};
struct Complex addComplex(struct Complex c1, struct Complex c2) {
struct Complex c;
c.real = c1.real + c2.real;
c.imag = c1.imag + c2.imag;
return c;
}
int main() {
struct Complex c1, c2, sum;
printf("Enter real and imaginary part of first complex number:\n");
scanf("%f %f", &c1.real, &c1.imag);
printf("Enter real and imaginary part of second complex number:\n");
scanf("%f %f", &c2.real, &c2.imag);
sum = addComplex(c1, c2);
printf("Sum = %.2f + %.2fi\n", sum.real, sum.imag);
return 0;
}
Enter real and imaginary part of first complex number:
2.5 3.0
Enter real and imaginary part of second complex number:
1.5 4.5
Sum = 4.00 + 7.50i
The structure Complex groups the real and imaginary parts of a complex number into a single unit so they can be passed together to functions. The function addComplex() receives two such structures, adds their corresponding components and returns a new Complex structure as the result.
In main(), the returned structure sum is used to display the final complex number in standard form. This example shows how structures and functions can be combined to implement clean, reusable operations on composite data types like complex numbers.
Write a C program to add two distances in the inch-feet system using structures.
Distance with members feet and inches.d1 and d2 of type Distance and read their values from the user.addDistance() that takes two Distance values and returns their sum as a Distance.addDistance(d1, d2) from main() and store the result in sum.
#include <stdio.h>
struct Distance {
int feet;
float inches;
};
struct Distance addDistance(struct Distance d1, struct Distance d2) {
struct Distance d;
d.feet = d1.feet + d2.feet;
d.inches = d1.inches + d2.inches;
if (d.inches >= 12) {
d.feet += (int)(d.inches / 12);
d.inches = d.inches - ((int)(d.inches / 12)) * 12;
}
return d;
}
int main() {
struct Distance d1, d2, sum;
printf("Enter first distance (feet and inches):\n");
scanf("%d %f", &d1.feet, &d1.inches);
printf("Enter second distance (feet and inches):\n");
scanf("%d %f", &d2.feet, &d2.inches);
sum = addDistance(d1, d2);
printf("Total Distance = %d feet %.2f inches\n", sum.feet, sum.inches);
return 0;
}
Enter first distance (feet and inches):
5 9.5
Enter second distance (feet and inches):
3 8.0
Total Distance = 9 feet 5.50 inches
The structure Distance represents a mixed unit measurement with separate fields for feet and inches. The function addDistance() adds the corresponding parts of two distances and then normalizes the result so that the inches part is always less than 12 by converting any excess inches into feet.
Returning a Distance structure from the function allows main() to receive both components of the result in a single value. This pattern reflects how composite measurements are handled in practice and shows the power of structures for organizing related data.