Struct in C: solved exercise with arrays of structs

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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

1
Average: 8.17

Common 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.

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.