Skip to main content

Student records with two functions

Short answer

Use struct student with name, roll, marks. readInfo() reads data with scanf, displayInfo() prints a formatted table. In main, declare struct student s[10], pass to both functions.

Create a structure named student that has name, roll and marks as members. Assume appropriate types and size of members. Use this structure to read and display records of 10 students. Create two functions: One is to read information of students and other to display the information.

Design. The structure student has name (char array of 30), roll (int) and marks (float). An array of 10 students is created in main() and passed to two functions: readInfo() reads the details and displayInfo() prints them in a table. As arrays are passed by address, readInfo() fills the original array.

#include <stdio.h>
#define N 10

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

void readInfo(struct student s[], int n)
{
    for (int i = 0; i < n; i++) {
        printf("Student %d (name roll marks): ", i + 1);
        scanf("%29s %d %f", s[i].name, &s[i].roll, &s[i].marks);
    }
}

void displayInfo(struct student s[], int n)
{
    printf("\n%-12s %-6s %s\n", "Name", "Roll", "Marks");
    for (int i = 0; i < n; i++)
        printf("%-12s %-6d %.1f\n", s[i].name, s[i].roll, s[i].marks);
}

int main(void)
{
    struct student s[N];
    readInfo(s, N);
    displayInfo(s, N);
    return 0;
}

Sample run

Input (entered one after another): Ram 1 78 Sita 2 85.5 Hari 3 62 Gita 4 91 Bibek 5 55.5 Maya 6 70 Kiran 7 66 Anita 8 88 Suman 9 49 Rita 10 73

Name         Roll   Marks
Ram          1      78.0
Sita         2      85.5
Hari         3      62.0
Gita         4      91.0
Bibek        5      55.5
Maya         6      70.0
Kiran        7      66.0
Anita        8      88.0
Suman        9      49.0
Rita         10     73.0

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 8 questions →