C Structure

Declaration of 'struct' in C.

* A structure is a user-defined compound data type which consists of data members related to a person or an item. It is referred to record in other programming languages.

Structure Declaration

In C, you can declare a structure using the 'struct' keyword. A structure is a user-defined data type that allows you to group together variables of different data types under a single name. Here's the basic syntax for declaring a structure:

struct structure_name {
    data_type member1;
    data_type member2;
    // More members...
};

Here's a breakdown of the elements in a structure declaration:

'struct': This keyword is used to define a structure in C.

'structure_name': Replace this with the desired name for your structure. This is the name you'll use to create variables of this structure type.

'data_type member1;', 'data_type member2;': These are the members (fields) of the structure. Each member has a data type (e.g., 'int', 'char', 'float', or another custom structure) and a name (e.g., 'member1', 'member2'). You can declare as many members as you need within the structure.

Here's an example of declaring a simple structure in C:

c Copy Code
#include <stdio.h>

struct student {
    int roll;
    char name[50];
    float percentage;
};

int main() {
    int n;
    printf("How many students' data do you want to store? (max. 9): ");
    scanf("%d", &n);

    if (n > 9 || n < 1) {
        printf("Please enter a number between 1 and 9.\n");
        return 1;  // Exit with an error code
    }

    struct student std[9]; // Use an array of size 9, as the maximum is 9.

    for (int i = 0; i < n; i++) {
        printf("\nEnter %d no. student roll: ", i + 1);
        scanf("%d", &std[i].roll);

        printf("Enter %d no. student name: ", i + 1);
        scanf("%49s", std[i].name);

        printf("Enter %d no. student percentage: ", i + 1);
        scanf("%f", &std[i].percentage);
    }

    printf("<---Data Store Completed--->\n");

    for (int i = 0; i < n; i++) {
        printf("\n%d no. student roll number: %d\n", i + 1, std[i].roll);
        printf("%d no. student name: %s\n", i + 1, std[i].name);
        printf("%d no. student percentage: %.2f\n", i + 1, std[i].percentage);
    }

    return 0;  // Exit with a success code
}
Output:
How many students' data do you want to store? (max. 9): 3

Enter 1 no. student roll: 21
Enter 1 no. student name: Ayan
Enter 1 no. student percentage: 64.78

Enter 2 no. student roll: 22
Enter 2 no. student name: Sanvi
Enter 2 no. student percentage: 84.45

Enter 3 no. student roll: 23
Enter 3 no. student name: Alan
Enter 3 no. student percentage: 78.89
<---Data Store Completed--->   

1 no. student roll number: 21  
1 no. student name: Ayan       
1 no. student percentage: 64.78

2 no. student roll number: 22  
2 no. student name: Sanvi      
2 no. student percentage: 84.45

3 no. student roll number: 23  
3 no. student name: Alan
3 no. student percentage: 78.89

This code is a basic example of how to use structures to store and manage data for multiple students. It includes error handling to ensure that the user enters a valid number of students. The program then collects and displays the student data accordingly.

What's Next?

We actively create content for our YouTube channel and consistently upload or share knowledge on the web platform.