Student Example


Explanation

A structure is used in C to group different types of data under a single name. It helps to store related information together. In this program, a structure named student is created to store student details such as name, roll number, and fees.

The structure variable s is used to access structure members. The program accepts student details from the user and displays all stored information.

How It Works?

  • Step 1: Create a structure named student with members name, rollno, and fees.
  • Step 2: Declare a structure variable s.
  • Step 3: Accept student name, roll number, and fees from the user.
  • Step 4: Store the entered values into structure members.
  • Step 5: Display all student details using the structure variable.

Example Program


#include <stdio.h>
#include <conio.h>

struct student
{

    char name[20];

    int rollno, fees;

};


main()
{

    struct student s;

    clrscr();


    printf("Enter the Name : ");

    scanf("%s", s.name);


    printf("\nEnter Roll No. : ");

    scanf("%d", &s.rollno);


    printf("\nEnter the Fees : ");

    scanf("%d", &s.fees);


    printf("\nStudent Name is %s", s.name);

    printf("\nRoll No. of the student is %d", s.rollno);

    printf("\nFees of the Student is %d", s.fees);


    getch();

}

Output

Enter the Name : Rahul

Enter Roll No. : 101

Enter the Fees : 25000

Student Name is Rahul
Roll No. of the student is 101
Fees of the Student is 25000

Practice Exercises

Task 1: Write a C program using structure to store and display employee details such as employee name, employee id, and salary.

Goal: Learn how to create a structure and access its members to store different types of information together.

💡 Hint: Create a structure named employee with members name, id, and salary. Create a structure variable, accept employee details from the user, and display all information.
💡 Show Solution

#include <stdio.h>
#include <conio.h>

struct employee
{

    char name[20];

    int id, salary;

};


main()
{

    struct employee e;

    clrscr();


    printf("Enter Employee Name : ");

    scanf("%s", e.name);


    printf("\nEnter Employee ID : ");

    scanf("%d", &e.id);


    printf("\nEnter Salary : ");

    scanf("%d", &e.salary);


    printf("\nEmployee Name is %s", e.name);

    printf("\nEmployee ID is %d", e.id);

    printf("\nEmployee Salary is %d", e.salary);


    getch();

}

Output
Enter Employee Name : Amit

Enter Employee ID : 101

Enter Salary : 35000

Employee Name is Amit
Employee ID is 101
Employee Salary is 35000
Explanation: The program creates a structure named employee to store employee information. The structure variable e stores the employee name, ID, and salary. After accepting values from the user, the program displays all employee details.