Unlocking the Power of C Programming: A Step-by-Step Guide

Structures and Arrays: A Perfect Combination

When it comes to storing and manipulating data in C programming, structures and arrays are two essential concepts that can be combined to achieve remarkable results. In this example, we’ll explore how to create a structure to store information about students and then use an array to store data for multiple students.

Defining the Structure

Let’s start by defining a structure called student that has three members: name (a string), roll (an integer), and marks (a float). This structure will serve as a blueprint for storing information about individual students.

typedef struct {
    char name[50];
    int roll;
    float marks;
} student;

Creating an Array of Structures

Next, we’ll create an array of structures s with 5 elements, allowing us to store information about 5 students. This array will enable us to efficiently manage and access data for multiple students.

student s[5];

Gathering User Input

Using a for loop, we’ll prompt the user to enter information about each of the 5 students. The program will then store this data in the array of structures. This interactive process ensures that our program is dynamic and can accommodate varying inputs.

for (int i = 0; i < 5; i++) {
    printf("Enter name of student %d: ", i + 1);
    scanf("%s", s[i].name);
    printf("Enter roll number of student %d: ", i + 1);
    scanf("%d", &s[i].roll);
    printf("Enter marks of student %d: ", i + 1);
    scanf("%f", &s[i].marks);
}

Displaying the Results

Once we’ve gathered all the necessary information, we’ll use another for loop to display the entered data on the screen. This output will provide a clear and concise summary of the stored information, making it easy to review and analyze.

for (int i = 0; i < 5; i++) {
    printf("Student %d:\n", i + 1);
    printf("Name: %s\n", s[i].name);
    printf("Roll Number: %d\n", s[i].roll);
    printf("Marks: %.2f\n\n", s[i].marks);
}

Putting it all Together

By combining structures and arrays, we’ve created a powerful program that can efficiently store and display information about multiple students. This example demonstrates the versatility of C programming and its ability to tackle complex data management tasks with ease.

Leave a Reply