In this article “Structure in C Programming”, we explain how a structure in C is used to group different types of data into a single logical unit.

Structure in C Programming

Introduction

A structure allows you to store data items of different data types (like int, char, float, etc.) together, which cannot be done using arrays.

What is a Structure in C?

A structure in C is a user-defined data type that combines variables of different data types into a single unit.
It is created using the keyword struct and can hold multiple members of various data types under one name.

Unlike arrays that store only similar data types, structures can store heterogeneous data.

Syntax of Structure

struct structure_name

{

    data_type member1;

    data_type member2;

    data_type member3;

    …

};

Explanation:

  • struct → A keyword used to declare a structure.
  • structure_name → Name of the structure (e.g., student, employee, book, etc.).
  • data_type → Can be int, float, char, etc.
  • member → Variables inside the structure. Each variable is called a member of the structure.

Example: Define Structure in C

struct student

{

    int roll_no;

    char name[20];

    float percent;

};

Here,

  • struct → Keyword
  • student → Name of the structure
  • Members → roll_no, name, and percent

This structure can hold a student’s roll number, name, and percentage — all of different data types.

How to Declare Structure Variables in C

To use a structure in your program, you must declare structure variables. These variables allow you to store and access data within the structure.

There are two methods to declare structure variables.

Method 1 – Using struct keyword inside main()

#include <stdio.h>

struct employee

{

    int id;

    char name[50];

    float salary;

};

void main()

{

    struct employee e1, e2;  // Structure variables declared

}

Here, struct employee e1, e2; creates two structure variables (e1 and e2) that can store data of type employee.

Method 2 – Declaring Variables at the End of Structure Definition

#include <stdio.h>

struct employee

{

    int id;

    char name[50];

    float salary;

} e1, e2;

This method declares structure variables (e1 and e2) at the same time as the structure definition.

How to Initialize Structure Members in C

A structure cannot be initialized at the time of its definition because it doesn’t get memory until a variable of that structure type is created.

Invalid Example:

struct data

{

    int p = 100;  // ❌ Error: Cannot initialize inside structure

    int q = 200;

};

Memory is allocated only when you declare a structure variable.

Method 1: Initialization During Declaration

#include <stdio.h>

struct student

{

    char name[40];

    int roll_no;

    int age;

};

void main()

{

    struct student s1 = {“Ram”, 70, 24};  // Structure initialization

    printf(“Name: %s\nRoll No: %d\nAge: %d”, s1.name, s1.roll_no, s1.age);

}

Output:

Name: Ram

Roll No: 70

Age: 24

Method 2: Using the Dot (.) Operator

#include <stdio.h>

#include <string.h>

struct student

{

    char name[40];

    int roll_no;

    int age;

};

void main()

{

    struct student s1;

    strcpy(s1.name, “Yash”);  // Assigning string to structure member

    s1.roll_no = 17;

    s1.age = 15;

    printf(“Name: %s\nRoll No: %d\nAge: %d”, s1.name, s1.roll_no, s1.age);

}

Output:

Name: Yash

Roll No: 17

Age: 15

Use of Structure in C

In C, built-in data types (like int, float, char) can store only one kind of value at a time.
However, in many real-life applications, we deal with objects that have multiple attributes of different types.

For example, to store student information like:

  • Roll Number (int)
  • Name (string)
  • Age (int)
  • Percentage (float)

we use a structure, since an array or single variable cannot handle mixed data types.

Example: Structure in C Program

#include <stdio.h>

#include <string.h>

struct student

{

    int roll_no;

    char name[30];

    float percentage;

};

void main()

{

    struct student s1;

    s1.roll_no = 101;

    strcpy(s1.name, “Amit Kumar”);

    s1.percentage = 88.5;

    printf(“Student Details:\n”);

    printf(“Roll No: %d\n”, s1.roll_no);

    printf(“Name: %s\n”, s1.name);

    printf(“Percentage: %.2f\n”, s1.percentage);

}

Output:

Student Details:

Roll No: 101

Name: Amit Kumar

Percentage: 88.50

Advantages of Structure in C

  1. Allows grouping of different types of data under one name.
  2. Easy to manage and maintain related information together.
  3. Simplifies passing multiple data items to functions.
  4. Multiple records of the same type can be stored using arrays of structures.

Disadvantages of Structure in C

  1. Becomes complex when used for large projects with many nested structures.
  2. Accessing data members individually can be slower.
  3. Cannot perform operations on an entire structure directly (must access each member separately).

Example: Array of Structures in C

You can store multiple records of the same type by creating an array of structures.

#include <stdio.h>

#include <string.h>

struct employee

{

    int id;

    char name[30];

    float salary;

};

void main()

{

    struct employee emp[3];

    int i;

    for (i = 0; i < 3; i++)

    {

        printf(“Enter ID, Name, and Salary of Employee %d:\n”, i + 1);

        scanf(“%d”, &emp[i].id);

        scanf(“%s”, emp[i].name);

        scanf(“%f”, &emp[i].salary);

    }

    printf(“\nEmployee Details:\n”);

    for (i = 0; i < 3; i++)

    {

        printf(“ID: %d, Name: %s, Salary: %.2f\n”, emp[i].id, emp[i].name, emp[i].salary);

    }

}

Output:

Enter ID, Name, and Salary of Employee 1:

101 Amit 35000

Enter ID, Name, and Salary of Employee 2:

102 Riya 40000

Enter ID, Name, and Salary of Employee 3:

103 John 28000

Employee Details:

ID: 101, Name: Amit, Salary: 35000.00

ID: 102, Name: Riya, Salary: 40000.00

ID: 103, Name: John, Salary: 28000.00

Conclusion

A Structure in C is a user-defined data type that allows combining data of different types under one name.
It helps represent real-world entities such as students, employees, or books more effectively.
Using structures improves data organization, code readability, and program modularity.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

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