Introduction
A structure in C allows you to store different types of data together under one variable name. For example, a student can have a name, age, marks, and roll number. Instead of creating separate variables for each piece of information, a structure groups related data into one unit. In this chapter, you will practice creating structures, accessing members, taking input, displaying records, calculating results, and working with multiple structure variables. Structures in C practice questions with solutions to help you understand the concepts.
Q1. Create and Display a Structure
Problem Statement
Write a C program to create a structure for a student and display the student’s name, age, and marks.
C Program
#include <stdio.h>
struct Student
{
char name[50];
int age;
float marks;
};
int main()
{
struct Student student = {"Rahul", 15, 85.5};
printf("Name = %s\n", student.name);
printf("Age = %d\n", student.age);
printf("Marks = %.2f", student.marks);
return 0;
}
Sample Output
Name = Rahul
Age = 15
Marks = 85.50
Explanation
We create a structure using:
struct Student
{
char name[50];
int age;
float marks;
};
This structure contains three members:
name → character array
age → integer
marks → float
We create a structure variable:
struct Student student;
In this example, we initialize it directly:
struct Student student = {"Rahul", 15, 85.5};
Structure members are accessed using the dot . operator:
student.name
student.age
student.marks
Concepts Covered
struct- Structure members
- Structure variable
- Dot
.operator - Structure initialization
Q2. Take Structure Data from the User
Problem Statement
Write a C program to create a student structure and take the student’s roll number, name, and marks as input.
C Program
#include <stdio.h>
struct Student
{
int roll;
char name[50];
float marks;
};
int main()
{
struct Student student;
printf("Enter roll number: ");
scanf("%d", &student.roll);
printf("Enter name: ");
scanf("%49s", student.name);
printf("Enter marks: ");
scanf("%f", &student.marks);
printf("\nStudent Details\n");
printf("Roll Number = %d\n", student.roll);
printf("Name = %s\n", student.name);
printf("Marks = %.2f", student.marks);
return 0;
}
Sample Output
Enter roll number: 101
Enter name: Rahul
Enter marks: 87.5
Student Details
Roll Number = 101
Name = Rahul
Marks = 87.50
Explanation
The structure contains:
struct Student
{
int roll;
char name[50];
float marks;
};
We create:
struct Student student;
To access the roll number:
student.roll
To access the name:
student.name
To access the marks:
student.marks
Notice that & is used with roll and marks in scanf():
scanf("%d", &student.roll);
scanf("%f", &student.marks);
For the character array name, we use:
scanf("%49s", student.name);
Concepts Covered
- Structure input
- Structure members
scanf()- Dot operator
- Character arrays
Q3. Store Employee Information in a Structure
Problem Statement
Write a C program to store an employee’s ID, name, and salary using a structure.
C Program
#include <stdio.h>
struct Employee
{
int id;
char name[50];
float salary;
};
int main()
{
struct Employee employee;
printf("Enter employee ID: ");
scanf("%d", &employee.id);
printf("Enter employee name: ");
scanf("%49s", employee.name);
printf("Enter salary: ");
scanf("%f", &employee.salary);
printf("\nEmployee Details\n");
printf("ID = %d\n", employee.id);
printf("Name = %s\n", employee.name);
printf("Salary = %.2f", employee.salary);
return 0;
}
Sample Output
Enter employee ID: 201
Enter employee name: Amit
Enter salary: 35000
Employee Details
ID = 201
Name = Amit
Salary = 35000.00
Explanation
The structure:
struct Employee
{
int id;
char name[50];
float salary;
};
groups three different data types together.
A structure does not require all members to have the same data type.
Here:
id → int
name → char array
salary → float
Concepts Covered
- Different data types in a structure
- Structure declaration
- Structure variable
- User input
Q4. Calculate Student Total and Average Using a Structure
Problem Statement
Create a student structure containing marks in three subjects. Calculate the total and average marks.
C Program
#include <stdio.h>
struct Student
{
char name[50];
int marks1;
int marks2;
int marks3;
};
int main()
{
struct Student student;
printf("Enter student name: ");
scanf("%49s", student.name);
printf("Enter marks in Subject 1: ");
scanf("%d", &student.marks1);
printf("Enter marks in Subject 2: ");
scanf("%d", &student.marks2);
printf("Enter marks in Subject 3: ");
scanf("%d", &student.marks3);
int total = student.marks1 +
student.marks2 +
student.marks3;
float average = total / 3.0;
printf("\nStudent = %s\n", student.name);
printf("Total = %d\n", total);
printf("Average = %.2f", average);
return 0;
}
Sample Output
Enter student name: Rahul
Enter marks in Subject 1: 80
Enter marks in Subject 2: 75
Enter marks in Subject 3: 90
Student = Rahul
Total = 245
Average = 81.67
Explanation
The structure stores the student’s information:
name
marks1
marks2
marks3
We calculate the total:
int total = student.marks1 +
student.marks2 +
student.marks3;
Then:
float average = total / 3.0;
Using 3.0 makes the division floating-point, so the decimal part is preserved.
Concepts Covered
- Structure members
- Arithmetic operations
- Floating-point division
- Student record
Q5. Store and Display Details of Multiple Students
Problem Statement
Write a C program to store the roll number, name, and marks of three students using an array of structures.
C Program
#include <stdio.h>
struct Student
{
int roll;
char name[50];
float marks;
};
int main()
{
struct Student students[3];
for (int i = 0; i < 3; i++)
{
printf("\nEnter details for Student %d\n", i + 1);
printf("Roll number: ");
scanf("%d", &students[i].roll);
printf("Name: ");
scanf("%49s", students[i].name);
printf("Marks: ");
scanf("%f", &students[i].marks);
}
printf("\nStudent Records\n");
for (int i = 0; i < 3; i++)
{
printf("\nStudent %d\n", i + 1);
printf("Roll = %d\n", students[i].roll);
printf("Name = %s\n", students[i].name);
printf("Marks = %.2f\n", students[i].marks);
}
return 0;
}
Sample Output
Enter details for Student 1
Roll number: 101
Name: Rahul
Marks: 85
Enter details for Student 2
Roll number: 102
Name: Aman
Marks: 91
Enter details for Student 3
Roll number: 103
Name: Priya
Marks: 88
Student Records
Student 1
Roll = 101
Name = Rahul
Marks = 85.00
Student 2
Roll = 102
Name = Aman
Marks = 91.00
Student 3
Roll = 103
Name = Priya
Marks = 88.00
Explanation
Instead of creating:
struct Student student1;
struct Student student2;
struct Student student3;
we create an array:
struct Student students[3];
Each element represents one complete student record.
For example:
students[0]
represents the first student.
Its members are accessed as:
students[0].roll
students[0].name
students[0].marks
Concepts Covered
- Array of structures
forloop- Structure members
- Multiple records
Q6. Find the Student with the Highest Marks
Problem Statement
Write a C program to store three students and find the student who has the highest marks.
C Program
#include <stdio.h>
struct Student
{
int roll;
char name[50];
float marks;
};
int main()
{
struct Student students[3];
for (int i = 0; i < 3; i++)
{
printf("\nEnter Student %d details\n", i + 1);
printf("Roll: ");
scanf("%d", &students[i].roll);
printf("Name: ");
scanf("%49s", students[i].name);
printf("Marks: ");
scanf("%f", &students[i].marks);
}
int highest = 0;
for (int i = 1; i < 3; i++)
{
if (students[i].marks > students[highest].marks)
{
highest = i;
}
}
printf("\nStudent with Highest Marks\n");
printf("Roll = %d\n", students[highest].roll);
printf("Name = %s\n", students[highest].name);
printf("Marks = %.2f", students[highest].marks);
return 0;
}
Sample Output
Enter Student 1 details
Roll: 101
Name: Rahul
Marks: 78
Enter Student 2 details
Roll: 102
Name: Aman
Marks: 92
Enter Student 3 details
Roll: 103
Name: Priya
Marks: 85
Student with Highest Marks
Roll = 102
Name = Aman
Marks = 92.00
Explanation
We initially assume the first student has the highest marks:
int highest = 0;
Then we compare the remaining students:
if (students[i].marks > students[highest].marks)
If a student has higher marks, we store that student’s index.
At the end:
students[highest]
contains the student with the highest marks.
Concepts Covered
- Array of structures
- Searching
- Structure members
- Comparison
- Index tracking
Q7. Calculate Employee Salary with Bonus
Problem Statement
Create an employee structure containing name and basic salary. Calculate the final salary after adding a bonus.
C Program
#include <stdio.h>
struct Employee
{
char name[50];
float salary;
};
int main()
{
struct Employee employee;
float bonus;
float finalSalary;
printf("Enter employee name: ");
scanf("%49s", employee.name);
printf("Enter basic salary: ");
scanf("%f", &employee.salary);
printf("Enter bonus: ");
scanf("%f", &bonus);
finalSalary = employee.salary + bonus;
printf("\nEmployee Name = %s\n", employee.name);
printf("Basic Salary = %.2f\n", employee.salary);
printf("Bonus = %.2f\n", bonus);
printf("Final Salary = %.2f", finalSalary);
return 0;
}
Sample Output
Enter employee name: Rahul
Enter basic salary: 30000
Enter bonus: 5000
Employee Name = Rahul
Basic Salary = 30000.00
Bonus = 5000.00
Final Salary = 35000.00
Explanation
The employee structure stores:
name
salary
The bonus is stored in a separate variable.
The final salary is calculated using:
finalSalary = employee.salary + bonus;
This example shows that structure members can be used in normal calculations.
Concepts Covered
- Structure
- Floating-point values
- Structure members
- Arithmetic operations
Q8. Compare Two Structure Variables
Problem Statement
Create a structure for two students and compare their marks.
C Program
#include <stdio.h>
struct Student
{
char name[50];
int marks;
};
int main()
{
struct Student student1;
struct Student student2;
printf("Enter first student name: ");
scanf("%49s", student1.name);
printf("Enter first student marks: ");
scanf("%d", &student1.marks);
printf("\nEnter second student name: ");
scanf("%49s", student2.name);
printf("Enter second student marks: ");
scanf("%d", &student2.marks);
if (student1.marks > student2.marks)
{
printf("\n%s has higher marks.", student1.name);
}
else if (student2.marks > student1.marks)
{
printf("\n%s has higher marks.", student2.name);
}
else
{
printf("\nBoth students have equal marks.");
}
return 0;
}
Sample Output
Enter first student name: Rahul
Enter first student marks: 85
Enter second student name: Aman
Enter second student marks: 92
Aman has higher marks.
Explanation
We create two structure variables:
struct Student student1;
struct Student student2;
Each variable contains its own:
name
marks
We compare:
student1.marks
with:
student2.marks
The structure itself contains multiple pieces of data, but individual members can be compared normally.
Concepts Covered
- Multiple structure variables
- Structure member access
if-else- Comparison
Q9. Pass a Structure to a Function
Problem Statement
Write a C program that passes a structure variable to a function and displays its information.
C Program
#include <stdio.h>
struct Student
{
int roll;
char name[50];
float marks;
};
void displayStudent(struct Student student)
{
printf("Roll = %d\n", student.roll);
printf("Name = %s\n", student.name);
printf("Marks = %.2f\n", student.marks);
}
int main()
{
struct Student student = {101, "Rahul", 88.5};
displayStudent(student);
return 0;
}
Sample Output
Roll = 101
Name = Rahul
Marks = 88.50
Explanation
The function is:
void displayStudent(struct Student student)
It receives a complete structure variable.
We call it using:
displayStudent(student);
Inside the function, the members are accessed using:
student.roll
student.name
student.marks
This is useful when a function needs to process a complete record.
Concepts Covered
- Structure as function argument
- Structure members
- Function
- Structure variable
Q10. Calculate Student Grade Using a Structure
Problem Statement
Create a student structure containing name and marks. Display the student’s grade based on the marks.
C Program
#include <stdio.h>
struct Student
{
char name[50];
float marks;
};
int main()
{
struct Student student;
printf("Enter student name: ");
scanf("%49s", student.name);
printf("Enter marks: ");
scanf("%f", &student.marks);
printf("\nStudent Name = %s\n", student.name);
printf("Marks = %.2f\n", student.marks);
if (student.marks >= 90)
{
printf("Grade = A");
}
else if (student.marks >= 75)
{
printf("Grade = B");
}
else if (student.marks >= 60)
{
printf("Grade = C");
}
else if (student.marks >= 40)
{
printf("Grade = D");
}
else
{
printf("Grade = F");
}
return 0;
}
Sample Output
Enter student name: Priya
Enter marks: 82
Student Name = Priya
Marks = 82.00
Grade = B
Explanation
The structure stores:
struct Student
{
char name[50];
float marks;
};
After taking input, we use the structure member:
student.marks
with an else-if ladder.
For example, if the marks are 82:
82 >= 90 → No
82 >= 75 → Yes
Therefore:
Grade = B
This example combines structures with conditional statements.
Concepts Covered
- Structures
- Structure members
else-if- Comparison
- Practical student record
Key Takeaways
- A structure is a user-defined data type in C.
- Structures group related data together.
- Structure members can have different data types.
- The
structkeyword is used to define a structure. - A structure variable can contain multiple related members.
- Use the dot
.operator to access members of a normal structure variable. - Structures can be initialized when they are created.
- Arrays of structures can store multiple records.
- Structures can be passed to functions.
- Structures are useful for student, employee, book, product, customer, and other records.
- A structure pointer uses the
->operator to access members. - Structures are an important foundation for larger C programs and data structures.
FAQs
1. What is a structure in C?
A structure is a user-defined data type that groups related variables of different data types under one name.
2. Why are structures used in C?
Structures are used to organize related information into a single record. For example, student information such as roll number, name, and marks can be stored together.
3. How do I access a structure member in C?
Use the dot . operator:
student.roll
student.name
student.marks
4. Can a structure contain different data types?
Yes. A structure can contain members such as int, float, char, arrays, and other supported C types.
5. Can I create an array of structures?
Yes.
struct Student students[10];
This creates an array capable of storing 10 Student structure variables.
6. Can a structure be passed to a function?
Yes. A structure can be passed directly to a function:
void display(struct Student student)
{
printf("%d", student.roll);
}
It can also be passed using a pointer.
7. What is the difference between . and -> in C structures?
The dot operator is used with a normal structure variable:
student.roll
The arrow operator is used with a pointer to a structure:
studentPtr->roll
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
