Write a program to show student database in sorted order using data structure.


Student database using structure data type starts with defining a structure data type named as student. This structure data type should have all the member variable to hold the attributes of a student. Name, department, roll number, marks etc are primary members. Year, enrolment date/year, address etc can be added. Member variable should be with proper type like roll should be integer, marks is floating point and other like name field can be strings or array of characters. This program is implemented with array of structure. Linked list implementation can have list type with student as the node.

Now coming to the main program. Main must have an array of variables of type student to hold them in memory. Dynamic array of linked list can be used for runtime allocation. First we should take inputs from user about the details of each students and fill the array. We have used for loop and scanf() function. Now to display in sorted order we have done a sorting on this array. Sorting requires swapping of records of two students. So a function swap_sudent_record() is written. This takes the records of two students as reference and swaps the content. Now once this array is sorted, we have to display the record list. We have used a for loop and used printf() function to display the details.

Source Code

/**************** Student database using structure data type ******************/
#include <stdio.h>
#include <string.h>

#define MAX_STRING   100
#define MAX_STUDENTS 5

/* Student record data structure */
struct student {
  char name[MAX_STRING];
  char dept[MAX_STRING];
  int rollno;
  float marks;

struct student s[MAX_STUDENTS];

/********************* Swaps two records of students ************************/
void swap_sudent_record (
  struct student *s1,
  struct student *s2 )
  float tf = 0;
  int ti = 0;
  char t[100];

  tf = s1->marks;
  s1->marks = s2->marks;
  s2->marks = tf;
  ti = s1->rollno;
  s1->rollno = s2->rollno;
  s2->rollno = ti;
  strcpy(t, s1->dept);
  strcpy(s1->dept, s2->dept);
  strcpy(s2->dept, t);
  strcpy(t, s1->name);
  strcpy(s1->name, s2->name);
  strcpy(s2->name, t);

/**************************** Main routine **********************************/
int main (
  int argc,
  char *argv[] )

  int i, j;
  printf ("\nStudent database using structure data type with sorting");
  printf ("\nEnter the name, dept, roll number and marks of %d students:\n",

  /* Collect all student records */
  for (= 0; i < MAX_STUDENTS; i++) {
    printf ("Student %d :", i + 1);
    scanf("%s %s %d %f", 

  for (= 0; i < MAX_STUDENTS; i++) {
    for(= i; j < MAX_STUDENTS; j++) {
      if (s[i].marks > s[j].marks) {
        swap_sudent_record (&s[i], &s[j]);

  printf("\nThe students are:");
  for (= 0; i < MAX_STUDENTS; i++) {
    printf ("\n%s %s %d %.2f",


Student database using structure data type with sorting
Enter the name, dept, roll number and marks of 5 students:
Student 1 :Student1 ECE 2 89.5
Student 2 :Student2 IT 1 90.0
Student 3 :Student3 CE 1 95.2
Student 4 :Student1 EE 3 88
Student 5 :Student5 ME 7 92

The students are:
Student1 EE 3 88.00
Student1 ECE 2 89.50
Student2 IT 1 90.00
Student5 ME 7 92.00
Student3 CE 1 95.20

Find More from our code collection
Armstrong number, binary number to a decimal number, bubble sort, decimal number to binary number, factorial of the given number factors, fibonacci numbers, HCF and LCM, matrix, mergesort, salary of the employee. palindrome, quadratic equation, star patterns, series etc. and much more...
#Return to Example Source Code