In this article Structure in C Programming we give the information about Structure is used to bind two or more different data types or data structures together into a single type.

Structure in C Programming:

Array in C language can be used to store only items of similar data types.

Struct in C is a user-defined data type. It is used to bind two or more different data types or data structures together into a single type. Structure is created using struct keyword and struct variable is created using struct keyword and struct tag name.

Structure is a user defined data type which is used to contain (combine) variables of different data types and keep them in a group.

Syntax:-

struct struct_name

{

Return_Type member1_name;

Data_Type member2_name;

Data_Type member3_name;

};

struct is a keyword with the help of which we create a new data type.

struct_name – This is the name of the structure data type, you can name it according to your own like – student, college, class etc.

DataType – DataType can include anything like int, char, float, double.

member1_name – This is the name of the variable. Every element located inside the structure is called a member.

define structure in c:

Example:

struct student

{

int roll_no;

char name[20];

float percent;

};

Here struct is the keyword and student is a new user defined data type inside which three struct member variables named roll_no, name and percent are declared with the help of int, char and float.If we have to access these three members, then for this we have to declare a variable of this student data type.

Structure in C Programming:

How to Declare Structure Variables in C

We declare the structure variable so that the structure can easily access the member and use it anywhere in our program.

Example –

struct employee

{

int id;

char name[50];

float salary;

};

Here struct is keyword and employee is a new user defined data type inside which three structure member variables namely id, name and salary are declared with the help of int, char and float. If we have to access these three members, then for this we have to declare a variable of this employee data type.

In C language, we can declare a structure variable in two ways:

Through struct keyword inside main() function.

At the end of the structure while defining the structure.

Method 1 -: By using struct keyword inside main() function.

#include<stdio.h>

struct employee

{

int id;

char name[50];

float salary;

};

void main()

{

struct employee e1, e2;

}

As I mentioned above, to access the Structure Member, we have to declare the Structure Variable.

So here in this example we have used struct employee e1, e2; By declaring two structure variables named e1, and e2, with the help of which we can easily access the structure member.

Method 2 -: At the end of the structure itself while defining the structure.

Example:

struct employee

{

int id;

char name[50];

float salary;

} e1,e2;

How to Initialize Structure Members in C

We cannot initialize the structure member with declaration. If we try to do so, the compiler will give error and the program will not run.

struct data

{

int p  = 100;  // COMPILER ERROR:  cannot initialize members here

int q  = 200;  // COMPILER ERROR:  cannot initialize members here

};

The reason for the error is that whenever we define a structure, the structure does not get any memory. Structure gets memory when we create structure variable.

We can initialize Structure members in these ways -:

Method 1 -:

#include<stdio.h>

struct student

{

char name[40];

int roll_no;

int age;

};

void main()

{

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

}

Method 2: Using Dot(.) Operator

#include<stdio.h>

struct student

{

char name[40];

int roll_no;

int age;

};

void main()

{

struct student s1;//initialization of each member separately

strcpy(s1.name, “Yash”) ;    //copying string into char arrays1.roll_no = 17;

s1.age = 15;

}

Note -: We access the structure member by dot (.) operator and initialize the value in them.

use of structure in c:

There are some pre-defined data types in C language, with the help of which we can store only one information related to one object at a time.

If we want to store any object like student’s roll number, name, age, percentage etc. which are different types of data, then we cannot do this with the help of predefined data type, so we need a structure.

We can store any type of data like int, char, float, double etc. in that new data type which we create through structure.

Advantages of Structure in C:

Through the structure, we can store different types of data values ​​together.

It is very easy to maintain because in this the entire record can be represented by a single name.

Through the structure, we can pass a complete record, function very easily.

If you want to store multiple records of the same type, you can create an array of structures.

Disadvantages of Structure in C:

When IT project becomes very complex then it becomes difficult to manage

Structure is slow because there is a need to store a lot of data in it.

You can retrieve one structure member at a time.

structure of c program with example:

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 *