What are Structures in C Programming & How to Declare

Hey guys, welcome, in this module, we are going to discuss what are structures in c Programming, their uses, their importance and needs, and the most important topic of this series.

So, let’s start with this wonderful concept of Structures in C programming.

What are Structures in C Programming?

OK, so we know about arrays in C Programming, Structures in C Programming are somewhat similar to that the only difference is that it allows storing the values of different data types. Therefore, structures are the collection of variables of different data types, i.e., they hold the values that belong to different data types under a single name. It is a user-defined data type in the C programming language.

What are Structures in C Programming

Let’s take an example to visualize the scenario.

Suppose, you want to store some information about a person like his/her name, mobile number, and salary. You can easily create different variables like name, mobNo, and salary, of respective data types and then you can store this information separately.

However, in the future, you want to store information about multiple persons let’s say 100. Now, you need to create different variables for storing the details of each information per person. i.e., name1, mobNo1, salary1, name2, mobNo2, salary2… likewise, it will go on till 100.

You can easily visualize that the code will become big and messy which can divert our concentration. Also, there will be no relation between variables(information) that would exist.

Instead of this, we can go for another approach, i.e., all the information can be collected under the single name Person, and which we can access for each and every single person. This will make our code look cleaner, will be readable, and make it efficient as well and we know that the collection of all related information under a single name is called a structure, in this case, all the data are collected under the name Person.

How to declare structures in C Programming?

Let’s see how to use structures in C programming i.e., how to declare the same while writing a program.

The struct keyword defines a structure type followed by an identifier (name of the structure that can be anything as per requirement). Then, you can declare one or more members or functions as per your requirements (i.e., declare variables inside curly braces) inside the curly braces of that structure.

Let’s understand this with an example.

struct Person
{
    char name[50];
    int age;
    float salary;
};

In the above Structures in C Programming example, we have declared a structure named Person with the struct keyword, and have defined 3 members inside it i.e., name, age, and salary of char, int, and float data type respectively.

What are the key points to remember while working with structures in C Programming?

There are some key points that the programmer should keep in mind while working with structures in C Programming in order to avoid errors.

  • No memory is allocated by the compiler when the structure is created.
  • The structure definition is like the blueprint for the creation of variables. In the same way, you can imagine it as a data type. When you define an integer as below:

int test1;

The int specifies that variable test1 can hold only integer elements only. Similarly, the definition of the structure only specifies that a structure variable can hold all that property when it is defined.

Always terminate the declaration of the structure with a semicolon otherwise it will throw an error.

How to define a Structure variable in C Programming?

Now since we have declared a structure, we have to declare a structure variable also, so that we can access the member of the structure.

We have declared a structure Person above in this module, now we are defining the structure variable for the same as:

Person per1;

Here, we have defined a structure variable per1 which is of type structure Person. The memory will be allocated only at that time when the structure variable is defined and the memory is allocated as per the byte system. Consider you have either a 32-bit or 64-bit system, the memory of float is 4 bytes, the memory of int is 4 bytes and the memory of char is 1 byte. Hence, 28 bytes of memory is allocated for structure per.

How to access the member of the structure?

Since we have declared the structure in C Programming and defined the structure variable, now how can we access the member of the structure, we can achieve this by using a dot ( . ) operator. Suppose you want to access the member age of the structure Person declared above and want to assign the value 50 to it then we can do this by following.

per1.age = 50;

In this manner, we can access the members of the structure and can assign a particular value to it.

Let’s see one example to understand the concept more clearly.

#include <stdio.h>
// structure Person declared
struct Person                 
{
    char name[50];
    float salary;
    int age;
};
int main()
{  
    // structure variable declared
    struct Person per1;          
    // Taking input from user
    printf("Enter name: ");        
    scanf("%s", &per1.name);
    printf("Enter the person age: ");
    scanf("%d", &per1.age);
    printf("Enter the salary of the person: ");
    scanf("%f", &per1.salary);
    
    // displaying the information
    printf("The details are: \n");       
    printf("Name : %s\n", per1.name);
    printf("Age : %d\n", per1.age);
    printf("Salary : %f\n", per1.salary);

    return 0;
}

The output of the above program is:

Enter name: Ram
Enter the person age: 23
Enter the salary of the person: 9000.40
The details are:
Name: Ram
Age: 23
Salary: 9000.40

In the above example, we have declared the structure and accessed each member of the structure using the structure variable, taken the input from the user, and displayed the same as the output.

I hope, you all enjoyed the What are Structures in C Programming module and got to learn many new and exciting concepts. So, guys, I recommend you to keep practising the program, and do some modifications and alterations.

Until they do, stay connected for much more exciting modules. Keep learning, Happy coding!