How to use Structures in C Programming Language

 How to use Structures in C Programming Language

Today in this blog post we are going to cover a simple data structure which is known as structure (struct) in C programming language. Basically it is a user defined data type with the help of which we can group related variables of different data types together under a single name known as a structure. With the help of structures we can merge or combine different data types like integer, characters, double, float, arrays, etc. into a single record.

In programming languages like C, C++, C#, etc., sometimes we encounter a situation where we need to store different variables with different datatypes, but these variables are related to one object or instance. In such situations, we use data structures. Today, we are going to discuss a simple technique that will help us store related variables in one place, namely, structures. We will look at how structures are used in the C programming language to help us achieve our target.



What is Structure in C Programming Language

We can consider structure as a special type of data structure that we can use to store data related to an entity under one umbrella and we can call it structure. We can consider a structure as a user-defined data type and it allows us to store various data types in one place. In structures, each data member used inside the structure body is called a member of a structure.

How we can create a Structure

In C programming language we can simply create a structure by using the struct keyword and declaring the members of the structure (variables and attributes) inside the structure body. In structures each member or variable that is declared is known as the member of structure. In order to declare or define a structure in our C program, we simply use a struct key word in the C programming language. Before going deep into structure concept let us look at the syntax and basic working of structures in C programming language.

Basic syntax and working of structures in C programming language


struct Student
{
	int Id;
	char Name[100];
};

Let us look at the basic syntax of the structure in the above coding syntax in C programming language. In order to create a structure we use the struct key word followed by the name of the structure Student and after that we have opening and closing curly brackets. With in those curly brackets we can declare and define multiple variables or members with different data types as per our requirement of the program like this,

int Id;
char Name[100];

Now the next point is how to use strucures in C programming language. In order to use a structure with in C programming language, in your main program or main() method simply create a variable of the structure like this, 

struct Student stu;

Now the next point is how can we access the members (variables declared inside the structure body). We can access the members of the structure by using the dot syntax (.) like this,

stu.Id = 1;

So after creating a variable of the structure we can simply access members of structure by simply writing structure of variable and after that simply insert a dot (.) after variable name. After using the dot syntax you can see the intellicence showing the members of structure and you can select any member you want to. 

So this is the working of structures in C programming language.


Simple coding example of structures in C programming language


#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
struct Student
{
	int Id;
	char Name[100];
};
void main()
{
	struct Student stu;
	stu.Id = 1;
	strcpy(stu.Name, "Muhammad Fahad");
	printf("Id = %d \n", stu.Id);
	printf("Name = %s \n", stu.Name);
	_getch();
}


In this void main function in the above coding example, we have simply created a variable of structure Student and named it as stu. Now we can use this stu object to access the data members of the Student structure with the help of the dot(.) operator like this,

 stu.Id = 1;

In the same way, we can access Id and Name attributes and assign values to them as shown in the coding example. We can directly assign the value of 1 to Id, but to assign value to the Name variable we have used the strcpy() function which is present in the string.h header file in C programming like this,

 strcpy(stu.Name, "Muhammad Fahad");

At last, we have simply printed the value of Id and Name to the console screen using printf() in C programming language.

printf("Id = %d \n", stu.Id);
printf("Name = %s \n", stu.Name);

At last, we have used the _getch() function to hold the console screen and not let it disappear after the successful execution of our program.


Congratulations! like always we have successfully implemented the concept of structures in C programming language.

Comments

Popular posts from this blog

How to Download and Install Microsoft visual studio community 2022 for free

Hello World Program In C Programming Language

How to add two numbers in C programming language