What is Struct in C programming? How it works?

C programming language offers a powerful feature called struct (short for structure) that allows you to group different types of variables under a single name. This is particularly useful when you want to represent a more complex entity that has multiple attributes. In this blog post, we’ll explore the basics of structs and demonstrate their use with three example programs.

What is a Struct?

A struct in C is a user-defined data type that groups together variables of different data types. These variables, known as members, are stored together in a contiguous block of memory.

Basic Syntax of a Struct

Here’s a basic syntax to define a struct in C:

struct StructName {
    dataType member1;
    dataType member2;
    // ... more members
};

Example 1: Defining and Using a Struct

Let’s start with a simple example. We will define a struct to represent a student and use it to store and display student information.

#include <stdio.h>

// Define a structure for a student
struct Student {
    char name[50];
    int age;
    float gpa;
};

int main() {
    // Create a student variable
    struct Student student1;

    // Input student details
    printf("Enter student's name: ");
    fgets(student1.name, 50, stdin);

    printf("Enter student's age: ");
    scanf("%d", &student1.age);

    printf("Enter student's GPA: ");
    scanf("%f", &student1.gpa);

    // Output student details
    printf("\nStudent Information:\n");
    printf("Name: %s", student1.name);
    printf("Age: %d\n", student1.age);
    printf("GPA: %.2f\n", student1.gpa);

    return 0;
}

Explanation

  1. Structure Definition: The struct Student defines a structure with three members: name, age, and gpa.
  2. Main Function:
  • A variable student1 of type struct Student is declared.
  • The program prompts the user to input the student’s name, age, and GPA.
  • The details of student1 are then printed to the console.

Example 2: Array of Structs

Now, let’s extend our example to handle multiple students by using an array of structs.

#include <stdio.h>

// Define a structure for a student
struct Student {
    char name[50];
    int age;
    float gpa;
};

int main() {
    int i;
    struct Student students[3];

    for(i = 0; i < 3; i++) {
        printf("Enter details for student %d:\n", i + 1);
        printf("Name: ");
        getchar(); // To consume the newline character left by previous input
        fgets(students[i].name, 50, stdin);

        printf("Age: ");
        scanf("%d", &students[i].age);

        printf("GPA: ");
        scanf("%f", &students[i].gpa);
    }

    // Output students details
    printf("\nStudent Information:\n");
    for(i = 0; i < 3; i++) {
        printf("Student %d:\n", i + 1);
        printf("Name: %s", students[i].name);
        printf("Age: %d\n", students[i].age);
        printf("GPA: %.2f\n\n", students[i].gpa);
    }

    return 0;
}

Explanation

  1. Array of Structs: An array students of type struct Student is declared to hold information for three students.
  2. Loop for Input: A loop is used to input details for each student.
  3. Loop for Output: Another loop is used to display the details of each student.

Example 3: Passing Structs to Functions

Finally, let’s see how to pass a struct to a function and return a struct from a function.

#include <stdio.h>

// Define a structure for a student
struct Student {
    char name[50];
    int age;
    float gpa;
};

// Function to input student details
struct Student inputStudent() {
    struct Student s;
    printf("Enter student's name: ");
    getchar(); // To consume the newline character left by previous input
    fgets(s.name, 50, stdin);

    printf("Enter student's age: ");
    scanf("%d", &s.age);

    printf("Enter student's GPA: ");
    scanf("%f", &s.gpa);

    return s;
}

// Function to display student details
void displayStudent(struct Student s) {
    printf("\nStudent Information:\n");
    printf("Name: %s", s.name);
    printf("Age: %d\n", s.age);
    printf("GPA: %.2f\n", s.gpa);
}

int main() {
    struct Student student1;

    student1 = inputStudent();

    displayStudent(student1);

    return 0;
}

Explanation

  1. Function to Input Struct: The inputStudent function prompts the user to input student details and returns a struct Student.
  2. Function to Display Struct: The displayStudent function takes a struct Student as an argument and prints the student details.
  3. Main Function: The main function calls inputStudent to get the student details and then calls displayStudent to display them.

Conclusion

Structs are a fundamental part of C programming, allowing you to group different types of variables together. This makes your code more organized and easier to manage. By practicing with these examples, you’ll gain a better understanding of how to define and use structs in your programs.

Happy coding! Feel free to leave any questions or comments below.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top