C Programs

C Programming – Structures


Programs at a Glance

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.

  1. Student Structure – Display Details of a Single Student
  2. Student Structure – Display Details of n Students Using Array of Structures
  3. Addition of Two Complex Numbers Using Structures and Functions
  4. Addition of Two Distances Using Structures

1. Student Structure – Display Details of a Single Student


Problem Statement

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-Friendly Algorithm
  1. Start the program.
  2. Define a structure Student with members: name, rollNo and grade.
  3. Declare a structure variable s of type Student.
  4. Read the name, roll number and grade from the user and store them in s.
  5. Display the stored name, roll number and grade in a formatted way.
  6. Stop the program.
C Program

#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;
}
    
Sample Output

Enter student name:
Ravi
Enter roll number:
101
Enter grade:
A

Student Details:
Name: Ravi
Roll Number: 101
Grade: A
    
Explanation

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.

2. Student Structure – Display Details of n Students Using Array of Structures


Problem Statement

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-Friendly Algorithm
  1. Start the program.
  2. Define a structure Student with members: name, rollNo and grade.
  3. Read the number of students n.
  4. Declare an array s of Student structures of size at least n.
  5. For each student from 1 to n, read and store name, roll number and grade in s[i].
  6. After input, iterate over the array and display the details of each student.
  7. Stop the program.
C Program

#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;
}
    
Sample Output

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
    
Explanation

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.

3. Addition of Two Complex Numbers Using Structures and Functions


Problem Statement

Write a C program to add two complex numbers by passing a structure to a function.

Student-Friendly Algorithm
  1. Start the program.
  2. Define a structure Complex with members real and imag for the real and imaginary parts.
  3. Declare two variables c1 and c2 of type Complex and read their values from the user.
  4. Write a function addComplex() that takes two Complex arguments and returns their sum as a Complex value.
  5. Inside the function, add the real parts and imaginary parts separately.
  6. Call addComplex(c1, c2) from main() and store the result in sum.
  7. Display the resulting complex number in the form a + bi.
  8. Stop the program.
C Program

#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;
}
    
Sample Output

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
    
Explanation

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.

4. Addition of Two Distances Using Structures


Problem Statement

Write a C program to add two distances in the inch-feet system using structures.

Student-Friendly Algorithm
  1. Start the program.
  2. Define a structure Distance with members feet and inches.
  3. Declare two variables d1 and d2 of type Distance and read their values from the user.
  4. Write a function addDistance() that takes two Distance values and returns their sum as a Distance.
  5. Inside the function, add feet and inches separately.
  6. If the inches part is 12 or more, convert the extra inches into feet and adjust both fields.
  7. Call addDistance(d1, d2) from main() and store the result in sum.
  8. Display the total distance in feet and inches.
  9. Stop the program.
C Program

#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;
}
    
Sample Output

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
    
Explanation

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.