C Structure

In C programming, a structure is a user-defined data type that allows grouping together variables of different types under a single name. This allows us to organize data in a way that reflects the relationship between different values, making the code easier to maintain and understand.

Definition

A structure is defined using the struct keyword, followed by the structure name, and then the variables (also called members) it will contain.

Syntax:

                        
struct structure_name {
    data_type member1;
    data_type member2;
    ...
};
                        
                    

Here, structure_name is the name of the structure, and member1, member2, etc., are the members that can be of different data types.

Example of Structure Declaration and Initialization

The following example demonstrates how to define a structure to represent a student with multiple members, such as name, age, and grade. It also shows how to initialize and access structure members.

Example 1:

                        
#include <stdio.h>
        
struct Student {
    char name[50];
    int age;
    char grade;
};
        
int main() 
{
    // Declare and initialize a structure variable
    struct Student student1 = {"John Doe", 20, 'A'};
            
    // Accessing structure members
    printf("Name: %s\n", student1.name);
    printf("Age: %d\n", student1.age);
    printf("Grade: %c\n", student1.grade);
    
    return 0;
}
                        
                    

Output:

Name: John Doe
Age: 20
Grade: A

Explanation of Example 1

In this example:

Structure Initialization

You can initialize structure variables in two main ways:

Example of Dynamic Structure Initialization

The following example demonstrates how to assign values to a structure after declaration:

Example 2:

                        
#include <stdio.h>
struct Student {
    char name[50];
    int age;
    char grade;
};

int main() {
    struct Student student2;

    // Dynamic Initialization
    printf("Enter name: ");
    fgets(student2.name, sizeof(student2.name), stdin);  // Using fgets for safe input
    printf("Enter age: ");
    scanf("%d", &student2.age);
    printf("Enter grade: ");
    scanf(" %c", &student2.grade); // space before %c to consume any leftover newline character

    // Accessing structure members
    printf("nStudent Details:n");
    printf("Name: %s\n", student2.name);
    printf("Age: %d\n", student2.age);
    printf("Grade: %c\n", student2.grade);        
        
        
    return 0;
}
                        
                    

Output:

Enter name: Alice Green
Enter age: 22
Enter grade: B
Student Details:

Name: Alice Green
Age: 22
Grade: B

Explanation of Example 2

In this example:

Nested Structures

Structures can also contain other structures, which is known as a nested structure. Here's an example:

Example 3: Nested Structures

                        
#include <stdio.h>
        
struct Date {
    int day;
    int month;
    int year;
};

struct Student {
    char name[50];
    int age;
    struct Date birthdate;  // Nested structure
};

int main() {
    struct Student student1 = {"John Doe", 20, {5, 7, 2002}};  // Initialize nested structure

    printf("Name: %s\n", student1.name);
    printf("Age: %d\n", student1.age);
    printf("Birthdate: %02d/%02d/%d\n", student1.birthdate.day, student1.birthdate.month, student1.birthdate.year);
       
    return 0;
}
                        
                    

Output:

Name: John Doe
Age: 20
Birthdate: 05/07/2002

Explanation of Example 3

In this example:

Key Points