C PROGRAMMING PROGRAM • STRUCTURES, UNIONS & ENUMS

Store and Display a Student Record Using a Structure

Learn how to store and display a student record using a structure using a clear C program.

IntermediatestructMembersRecord

PROBLEM UNDERSTANDING

Input and expected output

Sample input
101
Bhavya
89.5
Sample output
Roll: 101
Name: Bhavya
Marks: 89.50

COMPLETE C PROGRAM

Complete C implementation

student-record-structure.c
Open in compiler
#include <stdio.h>
#include <string.h>

struct Student
{
    int rollNumber;
    char name[100];
    float marks;
};

int main(void)
{
    struct Student student;

    printf("Enter roll number: ");
    scanf("%d", &student.rollNumber);
    getchar();
    printf("Enter name: ");
    fgets(student.name, sizeof student.name, stdin);
    student.name[strcspn(student.name, "\n")] = '\0';
    printf("Enter marks: ");
    scanf("%f", &student.marks);
    printf("Roll: %d\nName: %s\nMarks: %.2f\n", student.rollNumber, student.name, student.marks);
    return 0;
}

GUIDED CODE TOUR • NOT LIVE EXECUTION

Study the program line by line

Use the real compiler button above to run and debug with different inputs.

CURRENT STEP

Select Start to walk through the important lines.

SELECTED LINE

No line selected

EXPECTED OUTPUT FOR THE SAMPLE

Roll: 101
Name: Bhavya
Marks: 89.50
0%Step 0 of 0

PROGRAM EXPLANATION

Algorithm and explanation

  1. Read the required input values.
  2. Group related student fields in one structure and access them with the dot operator.
  3. Display the computed result.

Group related student fields in one structure and access them with the dot operator.

EFFICIENCY

Time and space complexity

Time complexity

O(1)

Auxiliary space

O(1)

DEBUGGING CHECKLIST

Common mistakes

Check this

Use the correct format specifier for every variable.

Check this

Initialize variables before using their values.

Check this

Check braces, semicolons and input order carefully.

Try it yourself

Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.