Advantages of Structure in C Programming

In C programming, structure is a collection of different data items which are referenced by single name. It is also known as user-defined data-type in C.

Using structure in C language has several benefits. In this article we are going to list key advantages of structure while programming in C.

  1. Heterogeneous collection of data items: structure allows us to create user defined data-type which can store items with different data types. For example, if we want to store students' records with their roll number, name, marks and address then it comprises items with different types i.e. roll number can be of integer number type, name is of string type, marks is floating number type and address is of string type.
    In C, it can be achieved by using structure like this:
    
    struct  student
    {
     int  roll_number;
     char  name[30];
     float  marks;
     char address[50];
    };
    

    Now structure variables can be created using struct student s1, s2; and these variables can store all the details of two students.
  2. Reduced complexity: think of storing student records of five different students with roll number, name, marks and address like in above example without structure! What do we need here? Five integer type variables for storing roll number of five students, five string variables for storing name of five students, five floating type variables for storing marks, and again five string variables for storing address of five students. Terrible! Don’t do it that way! Just create structure and create an array of structure. That’s it!
    Here is simple setup for doing that:
    
    struct  student
    {
     int  roll_number;
     char  name[30];
     float  marks;
     char address[50];
    };
    
    struct student s[5]; 
    
  3. Increased productivity: structure in C eliminates lots of burden while dealing with records which contain heterogeneous data items, thereby increasing productivity.
  4. Maintainability of code: using structure, we represent complex records by using a single name, which makes code maintainability like a breeze.
  5. Enhanced code readability: code readability is crucial for larger projects. Using structure code looks user friendly which in turn helps to maintain the project.
  6. Suitable for some mathematical operations: by using basic data-types we can not represent complex numbers, distances (feet-inch system), times (hour-minute-second system). Mathematical operations like adding complex numbers, or subtracting them; adding two distances in a feet-inch system; finding the difference between two time periods in an hour-minute-second system can be done effectively using the concept of structure.
    Here is simple setup for dealing with complex numbers:
    
    struct  complex
    {
     float real;
     float imaginary;
    };
    
    struct complex c1, c2, sum;
    

    Check C program to add two complex number using Structure and C program to add two distance in feet-inch system for real world examples.