Structures in C – Makes Coder Life Easy
So far, we discussed the various user-defined data types available in C such as union, enumeration, and typedef. But we are yet to discuss one of the most important user-defined data type called structures in C.
C Structures are the optimal way to represent data as a whole. We use structures to overcome the drawback of arrays.
We already know that arrays in C are bound to store variables that are of similar data types. Creating a structure gives the programmer the provision to declare multiple variables of different data types treated as a single entity.
In this tutorial, we will discuss:
- Significance of Structures in C
- Create a Structure in C
- Meaning of Array of Structures
- Simplify the syntax of structure using typedef
- Nested Structures in C
- Pass structures to a function
- Access Structures using pointers
- Difference between Structure in C and C++
- What are Structures in C?
In layman language, a structure is nothing but a cluster of variables that may be of different data types under the name name.
In programming terminology, a structure is a composite data type (derived from primitive data types such as int and float) that we use in order to define a collection of similar or different data types under one same name in a particular block of computer memory.
- Significance of Structures in C
Now that we understood the primary purpose of structures, let us acknowledge its significance by considering a very simple problem at hand.
Suppose you want to take the input of a particular date and display it. It would probably be a lot easier if we treat the records of the structure (day, month and year) as a single unit by logically relating them with each other.
Here is a program in C that illustrates how to solve the above problem:
#include <stdio.h>
struct date
{
unsigned int day, month, year;
};
int main()
{
struct date d = {12, 11, 2020}; // Here d is an object of the structure time
printf(“Welcome to DataFlair tutorials!\n\n”);
printf(“The Date is %d / %d / %d\n”, d.day, d.month, d.year);
return 0;
}
Code-
Output-
Now, let us consider another scenario where you want to store an employee’s records such as his name, age, and salary under a single unit. In this case, all the 3 records are of different types but can easily be clubbed into one with the help of structures.
Unveil the Difference between Structures and Unions in C
Here is a program in C that illustrates how to solve the above problem:
#include <stdio.h>
struct employee
{
char name[30];
int age;
float salary;
};
int main()
{
struct employee e = {“Rachel”, 29, 60000}; // Here e is an object of the structure employee
printf(“Welcome to DataFlair tutorials!\n\n”);
printf(“The name is: %s\n”,e.name);
printf(“The age is: %d\n”,e.age);
printf(“The salary is: %0.2f\n”,e.salary);
return 0;
}
Code-
Output-
Summary
In this tutorial, we paved the way to optimize data representation by mastering the concepts of structures by learning how to group together logically related variables. We discussed structures in detail by shedding light on its significance and the various applications using arrays, functions, and pointers.
Furthermore, if you have any query, feel free to ask in the comment section.
Reference
https://data-flair.training/blogs/structures-in-c/
GANGA BHAVANI
22VV1A1263
Information Technology




