The ‘struct’ Keyword in C Programming : Structures

In the world of C programming, the ‘struct’ keyword is a foundational tool for creating custom data structures and managing complex data efficiently. This comprehensive article aims to demystify the ‘struct’ keyword, elucidating its purpose, usage, and real-world examples with outputs. By the end, you’ll have a deep understanding of how ‘struct’ empowers you to work with structured data in your C code.

The Purpose of the ‘struct’ Keyword

The ‘struct’ keyword in C is used to define user-defined data types that can group together variables of different data types under a single name. These user-defined data structures, known as “structures,” allow you to represent complex entities more comprehensively.

Usage of the ‘struct’ Keyword

To define a structure, you use the ‘struct’ keyword followed by a user-defined name for the structure. Inside the structure, you specify the variables, or members, along with their data types. Here’s a basic example:

#include <stdio.h>
// Define a structure named 'Person'
struct Person {
    char name[50];
    int age;
    float height;
};

In this example, we’ve defined a ‘Person’ structure with three members: ‘name,’ ‘age,’ and ‘height.’ Each member has its own data type.

Example 1: Creating and Using a Structure

Let’s create instances of the ‘Person’ structure, initialize them with data, and print the information:

#include <stdio.h>
#include <string.h>
struct Person {
    char name[50];
    int age;
    float height;
};
int main() {
    // Create instances of 'Person' structure
    struct Person person1;
    struct Person person2;
    // Initialize data
    strcpy(person1.name, "Alice");
    person1.age = 25;
    person1.height = 165.5;
    strcpy(person2.name, "Bob");
    person2.age = 30;
    person2.height = 175.2;
    // Print information
    printf("Person 1:\nName: %s\nAge: %d\nHeight: %.1f\n", person1.name, person1.age, person1.height);
    printf("\nPerson 2:\nName: %s\nAge: %d\nHeight: %.1f\n", person2.name, person2.age, person2.height);
    return 0;
}

In this example, we create two instances of the ‘Person’ structure, initialize their data, and print the information.

Output:

Person 1:
Name: Alice
Age: 25
Height: 165.5

Person 2:
Name: Bob
Age: 30
Height: 175.2

Example 2: Using Structures in Arrays

Structures are also valuable for managing arrays of data. Let’s create an array of ‘Person’ structures and print the information for each person:

#include <stdio.h>
#include <string.h>
struct Person {
    char name[50];
    int age;
    float height;
};
int main() {
    // Create an array of 'Person' structures
    struct Person people[3];
    // Initialize data
    strcpy(people[0].name, "Charlie");
    people[0].age = 22;
    people[0].height = 170.8;
    strcpy(people[1].name, "David");
    people[1].age = 28;
    people[1].height = 182.3;
    strcpy(people[2].name, "Eve");
    people[2].age = 35;
    people[2].height = 155.9;
    // Print information for each person
    for (int i = 0; i < 3; i++) {
        printf("Person %d:\nName: %s\nAge: %d\nHeight: %.1f\n\n", i + 1, people[i].name, people[i].age, people[i].height);
    }
    return 0;
}

In this example, we create an array of ‘Person’ structures and populate them with data, then print the information for each person.

Output:

Person 1:
Name: Charlie
Age: 22
Height: 170.8

Person 2:
Name: David
Age: 28
Height: 182.3

Person 3:
Name: Eve
Age: 35
Height: 155.9
Author: user