Structures in C

Structure

Structure is a user defined data type which hold or store heterogeneous/different types data item or element in a single variable. It is a Combination of primitive and derived data type.
or
A structure is a collection of one or more data items of different data types, grouped together under a single name.
Variables inside the structure are called members of structure. Each element of a structure is called a member. struct keyword is used to define/create a structure. struct define a new data type which is a collection of different type of data. Syntax
struct structure_name /tag name
{
data_type varialbe1;
data_type varialbe2;
. . data_type varialben;
};
Note: Don't forget the semicolon }; in the ending line.
Example
struct teacher
{ int eid;
char name[50];
float salary;
char subject[20];
};

Where to Declare the Structure

structure can be declared above main or in side the main function. If structure is declared above main function that structure is a global structure then it can be accessible anywhere within the program. If it is declared in main function or any user defined function then it becomes a local structure whose scope is within the function only.

How to access structure members

In order to access the structure members first the sructure varialbe is declared. The syntax of structure variable declaraion is as follows
struct stucturename structurevariable;
ex : struct teacher t1;

Structure Demo Program


Program Output