Structures in C Programming

Structure is a collection of heterogeneous (i.e. of different types) data items which are referenced by a single name.

Structure is also known as records. A structure is a convenient way of grouping several data items together.

In C programming language, we can create and use data types other than the basic or fundamental data types. These are known as user defined data types. Structures are created by using struct keyword and can be defined as user defined data types.

Declaration of Structure & Structure Variables

Syntax for Declaring Structure


struct structure_name
{
 data_type member1;
 data_type member2;
 data_type member3;
  .
  .
  .
 data_type memberN;
};

Where structure_name and member can be any valid C identifier, data type can be any valid C data types. Structure name is also known as tag. When we declare a structure, new data type is defined. After declaring structure, structure variables can be created by using keyword struct and structure_name.

Syntax for Creating Structure Variables After Declaring Structure


struct structure_name  var1, var2, var3, …., varN;

Example: Declaration & Creation of Structure Variables

Let's create structure named student with member name, roll & marks:


struct  student
{
 char  name[30];
 int  roll;
 float  marks;
};

In this example, student is name of structure and it has three members, namely, name, roll and marks. Now struct student is name of new data type which can be used to create new structure variables.

For Example:


struct student  s1, s2, s3;

In the above example s1, s2, s3 are structure variables.


Structure variables can also be created at the time of structure declaration.

General syntax for declaring structure variables at the time of structure declaration is:


struct  structure_name
{
 data_type member1;
 data_type member2;
 data_type member3;
  .
  .
  .
 data_type memberN;
 
} var1, var2, var3, …., varN;

For Example


struct student
{
 char  name[30];
 int  roll;
 float  marks;
}s1, s2, s3;