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.
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
#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;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
Roll: 101 Name: Bhavya Marks: 89.50
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Group related student fields in one structure and access them with the dot operator.
- 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.
