C program to demonstrate example of structure of array

In this program, we will learn how to declare an array of some data type's variables within the structure, which is known as structure of array?

Example of structure of array in C

In this example, there is a structure named student having two arrays name and marks, name is the character array of 30 characters and marks is the integer array of 5 integers. name will store the name of the student and marks will store the marks of 5 subjects.

/*C program to demonstrate example of structure of array.*/

#include <stdio.h>

struct student {
    char name[30];
    int marks[5];
    int total;
    float perc;
};

int main()
{
    struct student std;
    int i;

    printf("Enter name: ");
    gets(std.name);

    printf("Enter marks:\n");
    std.total = 0;
    for (i = 0; i < 5; i++) {
        printf("Marks in subject %d ?: ", i + 1);
        scanf("%d", &std.marks[i]);
        std.total += std.marks[i];
    }
    std.perc = (float)((float)std.total / (float)500) * 100;

    printf("\nName: %s \nTotal: %d \nPercentage: %.2f", std.name, std.total, std.perc);

    return 0;
}

Output

Enter name: Mike
Enter marks:
Marks in subject 1? : 88
Marks in subject 2? : 98
Marks in subject 3? : 98
Marks in subject 4? : 78
Marks in subject 5? : 99

Name: Mike 
Total: 461 
Percentage: 92.20

C Structure & Union Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.