Struct in C: solved exercise with arrays of structs

  2 minutes

If you searched for struct in C solved exercise, this example helps you model real entities in C.

Define a Student struct with name and grade. Store multiple students in an array and compute average grade.

#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;
}
Average: 8.17
  • Incorrect field initialization.
  • Mixing up . and -> access.
  • Repeating logic instead of helper functions.

struct is the base for modeling domain entities (users, orders, events) in C.

If you want a complete path with progressive difficulty:

Yes. It targets patterns that commonly appear in practice assignments, technical interviews, and C programming exams.

In Programming in C in 100 Solved Exercises and C Exercises. Kindle Unlimited: View on Amazon.

Start with small inputs, run edge cases (empty, one item, max capacity), then rewrite the solution from scratch without copying.