Structures In C Language
C Structure is a collection of different data types which are grouped together and each element in a C structure is called member.

In real life we need to have different data types for ex: To maintain employee information. We should have information such as name, age, qualification, salary etc. here to maintain the information of employee dissimilar data types required. Name & qualification are char data type, age is integer, and salary is float. You can create this information separately but, better approach will be collection of this information under single name because all these information are related to person.

Structure is a collection of heterogeneous type of data i.e. different types of data. The various individual components, in a structure can be accessed and processed separately. A structure is a collection of variables referenced under a name, providing a convenient means of keeping related information. A structure declaration forms a template that may be used to create structure objects.

Difference between C variable, C array and C structure :

A normal C variable can hold only one data of one data type at a time.
An array can hold group of data of same data type.
A structure can hold group of data of different data types
Data types can be int, char, float, double and long double etc.

Structures
Features of Structures :

To copy elements of one array to another array of same data type elements are copied one by one. It is not possible to copy elements at a time. Where as in structure it is possible to copy the contents of all structure elements of different data types to another structure var of its type using assignment (=) operator. It is possible because structure elements are stored in successive memory locations.

Nesting of structures is also possible.

Defining a structure :

‘struct’ keyword is used to define a structure, struct define a new data type which is a collection of different type of data.

Syntax :

struct structure_name
{
//Statements
};

Example :

struct Book
{
char name[15];
float price;
int pages;
};

Here the struct Book declares a structure to hold the details of book which consists of three data fields, namely name, price and pages. These fields are called structure elements or members. Each member can have different data type, like in this case, name is of char type and price is of float type etc. Book is the name of the structure and is called structure tag.

Structure Initialization :

Like any other data type, structure variable can also be initialized at compile time.

  Example :
struct Patient
{
 float height;
 int weight;  
 int age; 
};
struct Patient p1 = { 180.75 , 73, 23 };    //initialization
or,
struct patient p1;
p1.height = 180.75;     //initialization of each member separately
p1.weight = 73;
p1.age = 23;
Accessing members of a structure :

There are two types of operators used for accessing members of a structure.
Member operator (.)
Structure pointer operator ()
Any member of a structure can be accessed as:

Syntax :   structure_variable_name.member_name

Suppose, if we want to access age for variable p1. Then, it can be accessed as :
P1.age;

  Program :WAP to define & initialize a structure & its variables
 #include<stdio.h>
struct book
 {
  char book1 [30];
  int pages; 
  float price;
  }; 


main ( )
{ 
struct book bk1 = { “c++”, 300, 285};
printf (“ \n Book name   : %s “ , bk1.book1);
printf (“\n No of pages   :%d “,bk1.pages);
printf (“\n book price     :%f “,bk1.price); 
}
Output :

Book name: C ++
No of pages: 300
Book Price: 285