Struct in C: solved exercise
If you searched for struct in C solved exercise, this example helps you model real entities in C.
Problem statement
Define a Student struct with name and grade. Store multiple students in an array and compute average grade.
C solution
#include <stdio.h>
typedef struct {
char name[20];
float grade;
} Student;
int main(void) {
Student s[] = {
{"Ana", 8.0f},
{"Luis", 7.5f},
{"Marta", 9.0f}
};
float sum = 0.0f;
int n = sizeof(s) / sizeof(s[0]);
for (int i = 0; i < n; i++) sum += s[i].grade;
printf("Average: %.2f\n", sum / n);
return 0;
}Expected output
Average: 8.17Common mistakes
- Incorrect field initialization.
- Mixing up
.and->access. - Repeating logic instead of helper functions.
Practical use
struct is the base for modeling domain entities (users, orders, events) in C.
Recommended next exercise
- Pointers in C: solved pass-by-reference exercises
- Malloc and free in C: solved dynamic memory exercise
- Binary search in C: solved exercise on sorted arrays
- All C exercises
Guided practice and full book
If you want a complete path with progressive difficulty:
FAQ
Is this exercise useful for C exams and technical interviews?
Yes. It targets patterns that commonly appear in practice assignments, technical interviews, and C programming exams.
Where can I keep practicing with more solved C exercises?
In Programming in C in 100 Solved Exercises and C Exercises. Kindle Unlimited: View on Amazon.
How should I practice this exercise type to improve faster?
Start with small inputs, run edge cases (empty, one item, max capacity), then rewrite the solution from scratch without copying.