PART B · PRESCRIBED EXERCISE 08

Read and display a student structure

Understand the algorithm. Run the code.
Explain the result with confidence.

Read the programOpen the playground
Aim & definitionStep-by-step algorithmWorked sampleExtra test cases3 viva answers

LAB B08 · 01

Understand the task.

Aim: Read and display a student structure.

DEFINITION

Student record. A structure groups related members, possibly of different types, into one record. Each structure object has its own member storage.

Review the related theory in Unit 3.

LAB B08 · 02

Know what goes in and what comes out.

Three lines: a roll number from 1 to 999; a nonempty name of at most 60 characters; and marks from 0 to 100, including a decimal part if needed.

Sample input

7
Ananya Rao
85.5

Expected output

Roll = 7
Name = Ananya Rao
Marks = 85.50

Source programs read the documented input directly, without prompt text, so the same sample works in the embedded compiler’s STDIN field.

LAB B08 · 03

Plan the steps before writing code.

  1. Define struct Student with roll, name and marks members.
  2. Declare one struct Student object.
  3. Read and validate student.roll, then consume the rest of that input line.
  4. Read the name with fgets, remove its newline and validate its length.
  5. Read and validate student.marks.
  6. Display the record using the dot member-access operator.

LAB B08 · 04

One complete program. Ready to run.

Read and display a student structuremain.c
#include <stdio.h>
#include <string.h>
struct Student {
    int roll;
    char name[62];
    double marks;
};
int main(void)
{
    struct Student student;
    int ch;
    if (scanf("%d", &student.roll) != 1 || student.roll < 1 || student.roll > 999) {
        puts("Enter a roll number from 1 to 999."); return 1;
    }
    while ((ch = getchar()) != '\n' && ch != EOF) { }
    if (fgets(student.name, sizeof student.name, stdin) == NULL) {
        puts("Enter a student name."); return 1;
    }
    student.name[strcspn(student.name, "\n")] = '\0';
    if (strlen(student.name) == 0 || strlen(student.name) > 60) {
        puts("Use a nonempty name of at most 60 characters."); return 1;
    }
    if (scanf("%lf", &student.marks) != 1 || !(student.marks >= 0.0 && student.marks <= 100.0)) {
        puts("Enter marks from 0 to 100."); return 1;
    }
    printf("Roll = %d\nName = %s\nMarks = %.2f\n", student.roll, student.name, student.marks);
    return 0;
}
EXPECTED OUTPUTRoll = 7 Name = Ananya Rao Marks = 85.50

Try this programDownload B08-student-structure.c
Compile on your own computer
gcc B08-student-structure.c -o program

Run the generated program and enter the sample input. The examples use standard C90-compatible syntax and explicit function declarations. For these learning programs, enter numeric values representable in the stated C types and within the documented ranges.

LAB B08 · 05

Connect each statement to its purpose.

  • struct Student defines the record type; struct Student student declares an object of that type.
  • The dot operator accesses a member of the object: student.roll, student.name and student.marks.
  • scanf needs the address of numeric members. The name array already converts to a pointer for fgets.
  • Consuming the roll-number line prevents its leftover newline from becoming an unintended empty name.
  • A bounded fgets read preserves spaces in a name and allows validating the length.
  • The example reads one record. An array of structures extends this approach to several students.

LAB B08 · 06

Follow the sample calculation.

Step or componentWhat happens
Roll linestudent.roll = 7
Name linestudent.name contains Ananya Rao and its null terminator.
Marks linestudent.marks = 85.5
DisplayPrint the three associated fields from one object.

LAB B08 · 07

Check more than one input.

Try this input or caseExpected behaviour
Roll 1, name A, marks 0Accepted.
Roll 999, 60-character name, marks 100Accepted.
Empty name, roll 0 or marks above 100Rejected at the corresponding validation step.

Time and storage

O(L) name processing for name length L; one fixed-size record plus constant working space.

Start with the sample, then test a boundary and a case that changes the result. A correct sample alone does not establish that every input is handled correctly.

LAB B08 · 08

Use the real C compiler.

Choose an example and load it into the embedded C compiler. Select I/O inside the compiler, check the sample STDIN, and press Run. Change the input or edit the source to explore a different case.

Sample input (STDIN)

7
Ananya Rao
85.5

Expected output

Roll = 7
Name = Ananya Rao
Marks = 85.50

Load the compiler when you’re ready to practise.

Your C workspace

Edit real C code and see real compiler output.

Compiler provided by OneCompiler. Opening the editor connects to that service; code and input you run are processed there. If the embed is unavailable, open the C compiler in a new tab and paste the source below.

View / copy the selected source
#include <stdio.h>
#include <string.h>
struct Student {
    int roll;
    char name[62];
    double marks;
};
int main(void)
{
    struct Student student;
    int ch;
    if (scanf("%d", &student.roll) != 1 || student.roll < 1 || student.roll > 999) {
        puts("Enter a roll number from 1 to 999."); return 1;
    }
    while ((ch = getchar()) != '\n' && ch != EOF) { }
    if (fgets(student.name, sizeof student.name, stdin) == NULL) {
        puts("Enter a student name."); return 1;
    }
    student.name[strcspn(student.name, "\n")] = '\0';
    if (strlen(student.name) == 0 || strlen(student.name) > 60) {
        puts("Use a nonempty name of at most 60 characters."); return 1;
    }
    if (scanf("%lf", &student.marks) != 1 || !(student.marks >= 0.0 && student.marks <= 100.0)) {
        puts("Enter marks from 0 to 100."); return 1;
    }
    printf("Roll = %d\nName = %s\nMarks = %.2f\n", student.roll, student.name, student.marks);
    return 0;
}

LAB B08 · 09

Explain why the program works.

Why use a structure instead of unrelated variables?

The members belong to one student record and can be managed together using a single object.

Is name a pointer member?

No. It is a character array embedded in the structure object.

Which operator accesses members through a structure pointer?

Use ->, for example p->roll. This program uses an object directly, so it uses the dot operator.

LAB B08 · 10

Build the next skill.

Read the viva answers, try the extra cases, and return to the lab index to mark this exercise as practised.