C Program to Store Information of a Student Using Structure

Created with Sketch.

C Program to Store Information of a Student Using Structure

Introduction

In this C programming tutorial, we will create a program to store information about a student using a structure. A structure is a composite data type in C that allows you to group variables of different data types under a single name. We will design a structure to represent student details, take input from the user, store the information, and display it. The algorithm, C code, and explanation will be provided.

Understanding the Algorithm

The algorithm for the program involves the following steps:

  1. Define Structure: Create a structure to hold information about a student, including attributes like name, roll number, marks, etc.
  2. Input Student Information: Accept details of a student such as name, roll number, and marks from the user.
  3. Store Information: Create a variable of the structure type and store the input information in that variable.
  4. Display Information: Print or display the stored information about the student.

C Program for Storing Student Information

#include <stdio.h>

// Define structure for student information
struct Student {
    char name[50];
    int rollNumber;
    float marks;
};

int main() {
    // Declare a variable of the structure type
    struct Student student;

    // Input student information
    printf("Enter student details:\n");
    printf("Name: ");
    fgets(student.name, sizeof(student.name), stdin);
    printf("Roll Number: ");
    scanf("%d", &student.rollNumber);
    printf("Marks: ");
    scanf("%f", &student.marks);

    // Display student information
    printf("\nStudent Information:\n");
    printf("Name: %s", student.name);
    printf("Roll Number: %d\n", student.rollNumber);
    printf("Marks: %.2f\n", student.marks);

    return 0;
}

Output Example

Example: Input and Display Student Information

Enter student details:
Name: John Doe
Roll Number: 12345
Marks: 85.5

Student Information:
Name: John Doe
Roll Number: 12345
Marks: 85.50

Explanation

  1. Structure Definition: We define a structure named Student with attributes name, rollNumber, and marks.
  2. Input: The program prompts the user to input details like name, roll number, and marks using printf and scanf functions.
  3. Storing Information: An instance of the structure Student named student is declared, and the input information is stored in it.
  4. Display: The program then displays the stored information using printf.

Conclusion

This C program demonstrates the use of structures to store information about a student. You can extend this program to include additional attributes or create an array of structures to store information for multiple students.

Feel free to ask if you have any questions or need further clarification!

Leave a Reply

Your email address will not be published. Required fields are marked *