C Program to Store Information (name, roll and marks) of a Student Using Structure

Updated on September 30, 2024
Store Information (name, roll and marks) of a Student Using Structure header image

Introduction

Working with complex data types in the C programming language often involves creating structured forms of data collections. One of the most valuable data types in C for managing related data is the structure (struct). A structure allows you to store different data types together under a single umbrella. This is especially useful for organizing records like student information, which includes a name, roll number, and marks.

In this article, you will learn how to define and utilize structures in C to manage student data effectively. Discover how to define a structure, store data in it, and display this information, all using clear examples to guide you.

Defining the Structure

Creating the Student Structure

  1. Define a structure named Student to hold a student's name, roll number, and marks.

  2. Include the standard input-output library header stdio.h for input and output operations.

    c
    #include <stdio.h>
    
    struct Student {
        char name[50];
        int roll;
        float marks;
    };
    

    This block of code defines a new structure type called Student, which can now be used to create variables that can store a student's name, roll number, and marks. The name is stored as an array of characters.

Inputting Data into the Structure

Prompt User Input to Fill the Structure

  1. Declare a Student variable.

  2. Prompt the user to enter the details of the student.

    c
    int main() {
        struct Student student1;
    
        printf("Enter student's name: ");
        fgets(student1.name, sizeof(student1.name), stdin);
    
        printf("Enter roll number: ");
        scanf("%d", &student1.roll);
    
        printf("Enter marks: ");
        scanf("%f", &student1.marks);
    
        return 0;
    }
    

    In this code, fgets is used to read a string input, which includes spaces, from the user. The scanf function reads the integer and float values for the roll number and marks. Note the use of & in scanf for numerical inputs to provide the address where the inputted value will be stored.

Displaying Data

Output the Data Stored in the Structure

  1. Print out the information stored in the structure to verify correct input.

    c
    int main() {
        struct Student student1;
    
        // Input code here as previously shown
    
        printf("\nStudent's Name: %s", student1.name);
        printf("Roll Number: %d\n", student1.roll);
        printf("Marks: %.2f\n", student1.marks);
    
        return 0;
    }
    

    This snippet outputs the data entered by the user. The use of %.2f in the printf statement for displaying marks formats the float to two decimal places, enhancing the readability of the output.

Conclusion

Understanding and using structures in C programming enable efficient management of grouped data. In this guide, by defining a Student structure, you learned to input and display complex and varied data types cohesively. This approach is fundamental when dealing with records or collections of related information. Apply these concepts to create structures for different data handling scenarios to keep your programs organized and maintainable. Through these structures, your code not only becomes cleaner but also easier to manage and scale.