---
title: "Student records with two functions"
subject: "Computer Programming"
chapter: 8
key: "semester_1/computer_programming:struct-student-read-display-functions"
source: https://www.bctengineer.com/q/semester_1/computer_programming/struct-student-read-display-functions
site: BCT Engineering
---

# Student records with two functions

Computer Programming · Chapter 8: Structures

- Asked in: 2074 Chaitra, 6 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/struct-student-read-display-functions
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-8

## Question

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.

## 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.

## Answer

**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.

```c
#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`

```text
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
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Which members does student have?
   A. Only name
   B. name and age
   C. roll and address
   D. name (char array), roll (int), marks (float)
   Answer: D. These match the question. (Key point: Structure)

2. What are the two functions for?
   A. readInfo() reads details, displayInfo() prints them
   B. Both read
   C. Both print
   D. One sorts, one searches
   Answer: A. Separate input and output tasks. (Key point: Two functions)

3. Why does readInfo() fill the original array?
   A. Arrays are passed by value
   B. Arrays are passed by address
   C. It returns the array
   D. It uses global variables
   Answer: B. Changes in the function affect the original. (Key point: Passing)

4. How is marks read for s[i]?
   A. s[i].marks
   B. &s.marks
   C. &s[i].marks
   D. s->marks
   Answer: C. scanf needs the address of a float. (Key point: Scanf)

## Related in Computer Programming

- [Pass student array; display passed students](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-whole-array-student-info) — Computer Programming, chapter 7
- [Display records sorted by name from file](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-sorted-records-by-name) — Computer Programming, chapter 10
- [96 students record; pass marks](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-96-students-ioe-txt) — Computer Programming, chapter 10
- [College records with nested date structure](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-college-estdate) — Computer Programming, chapter 8
- [Write student records to STUDENT.DAT](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-student-dat) — Computer Programming, chapter 10
- [Read records until end; show nth record](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-worker-nth-record) — Computer Programming, chapter 10, asked 2×

## Related in other subjects

- [Student file read/write and modify by roll](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-file-modify-roll) — Object Oriented Programming, chapter 8
- [Student records file with CRN search](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-crn-file-search) — Object Oriented Programming, chapter 8
- [Student file displayed in descending marks order](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-file-descending) — Object Oriented Programming, chapter 8
- [File stream; student binary file program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afile-stream-student-binary) — Object Oriented Programming, chapter 8
