Back to C Programming
2026-07-155 min read

Store Information of a Student Using Structure

Learn Store Information of a Student Using Structure step by step with clear examples and exercises.

Why This Matters

In programming, managing data efficiently is crucial, especially when dealing with complex data like student records. Structures in C allow us to create user-defined data types that can hold multiple pieces of related data together, making it easier to manage and manipulate the data. This skill is essential for handling real-world scenarios such as maintaining records in a school or college.

Prerequisites

Before diving into this lesson, you should have a good understanding of:

  1. Basic C syntax (variables, constants, operators)
  2. Control structures (if-else, switch-case)
  3. Arrays
  4. Input/Output operations using standard library functions like printf() and scanf()
  5. Pointers (optional but recommended for understanding function arguments and dynamic memory allocation)

Core Concept

Defining a Student Structure

To create a structure for storing student data, we first define the structure with appropriate member variables:

struct Student {
char name[50]; // Student's name
int roll; // Roll number
float marks; // Total marks obtained
};

Here, Student is our custom data type containing three members: name, roll, and marks.

Creating a Variable of the Structure Type

Next, we create a variable of the structure type to store student information:

struct Student s; // Declaring a variable 's' of type 'Student'

Inputting and Displaying Student Information

Now, let's write functions to input and display student information using the scanf() and printf() functions. We will also include error handling for invalid inputs:

void get_student_info(struct Student *student) {
printf("Enter name: ");
scanf("%[^\n]s", student->name);

printf("Enter roll number: ");
int inputRoll;
if (scanf("%d", &inputRoll) != 1) {
printf("Invalid input. Please enter an integer for roll number.\n");
return;
}
student->roll = inputRoll;

printf("Enter marks: ");
float inputMarks;
if (scanf("%f", &inputMarks) != 1) {
printf("Invalid input. Please enter a floating-point number for marks.\n");
return;
}
student->marks = inputMarks;
}

void display_student_info(struct Student student) {
printf("\nName: %s\nRoll Number: %d\nMarks: %.2f\n", student.name, student.roll, student.marks);
}

Main Function

Finally, let's write the main function that calls these functions to get and display student information:

int main() {
struct Student s;

printf("Enter student information:\n");
get_student_info(&s);

printf("\nStudent Information:\n");
display_student_info(s);

return 0;
}

Worked Example

Let's run the program and input some sample data:

#include <stdio.h>

struct Student {
char name[50];
int roll;
float marks;
};

void get_student_info(struct Student *student) {
printf("Enter name: ");
scanf("%[^\n]s", student->name);

printf("Enter roll number: ");
int inputRoll;
if (scanf("%d", &inputRoll) != 1) {
printf("Invalid input. Please enter an integer for roll number.\n");
return;
}
student->roll = inputRoll;

printf("Enter marks: ");
float inputMarks;
if (scanf("%f", &inputMarks) != 1) {
printf("Invalid input. Please enter a floating-point number for marks.\n");
return;
}
student->marks = inputMarks;
}

void display_student_info(struct Student student) {
printf("\nName: %s\nRoll Number: %d\nMarks: %.2f\n", student.name, student.roll, student.marks);
}

int main() {
struct Student s;

printf("Enter student information:\n");
get_student_info(&s);

printf("\nStudent Information:\n");
display_student_info(s);

return 0;
}

Output:

Enter student information:
Enter name: Jack
Enter roll number: 23
Enter marks: 34.5

Student Information:
Name: Jack
Roll Number: 23
Marks: 34.50

Common Mistakes

  1. Forgetting to include the header file: Make sure you have #include at the beginning of your code.
  2. Incorrect use of scanf(): Be careful with the format specifiers in scanf(). In our example, we used %[^\n]s to read a string without newline characters.
  3. Not passing the correct argument type to functions: When calling get_student_info(), make sure you pass the address of the structure variable using the address-of operator (&).
  4. Displaying incorrect student information: In display_student_info(), remember to use the dot (.) notation when accessing members of a local structure variable.
  5. Incorrect data types for roll number and marks: Ensure that you have defined roll as an integer and marks as a floating-point number.
  6. Invalid input handling: Make sure your input functions handle invalid inputs gracefully, such as non-integer values for roll numbers or negative marks.

Practice Questions

  1. Modify the program to store information of multiple students using arrays of structures.
  2. Write a function to sort an array of student structures based on roll numbers or marks obtained.
  3. Implement a function that calculates and displays the average marks of all stored students.
  4. Write a function to search for a specific student by their roll number.
  5. Modify the program to handle cases where the user enters invalid input (e.g., non-integer values for roll numbers or negative marks).
  6. Implement a function that adds new student records to an existing array of structures, ensuring that there is enough memory allocated to store the new record.
  7. Write a function that removes a student from an array of structures based on their roll number.
  8. Modify the program to handle cases where the user wants to sort students by name instead of roll number or marks obtained.
  9. Implement a function that displays all student records in ascending order of roll numbers, marks obtained, and then names.
  10. Write a function that finds the student with the highest marks in the array of structures.

FAQ

  1. Why do we use structures in C?

Structures allow us to create custom data types that can hold multiple pieces of related data together, making it easier to manage and manipulate the data.

  1. What is the difference between a structure variable and a structure type?

A structure type defines the layout of the structure members, while a structure variable is an instance of that structure type that holds actual data.

  1. Why do we use pointers when working with structures in C?

Pointers are used to pass structure variables as arguments to functions and to dynamically allocate memory for structures.

  1. How can I initialize a structure variable in C?

You can initialize a structure variable directly using curly braces {} or indirectly by assigning values to each member separately.

  1. What are some common mistakes when working with structures in C?

Some common mistakes include forgetting to include the header file, incorrect use of scanf(), not passing the correct argument type to functions, displaying incorrect student information, and using incorrect data types for roll numbers and marks. Additionally, remember to handle invalid inputs gracefully.