Structure in C – free C programming tutorial

In this tutorial you will be familiarized with basics of structure available in C programming language along with the syntax of declaring & accessing them and example program .

Structure :

Structures in C are defined as user defined data type available in C language which allows to combine data of different datatypes.
As we know arrays allow to define type of variable that can hold several data items of the same kind.
Similarly structures are user defined data types available in C language which allows to combine data of different datatypes under a same variable.
By this basic feature of structures i.e. to hold different datatypes, structures are normally used to maintain records.
All the data elements present in structures are known as members.

Let us assume that you have to maintain the record of  employers of the company , so attributes we want to keep in mind are given as follows :

  1. Name of the employee (string)
  2. Employee ID ( integer / unsigned integer / long)
  3. Designation (string)
  4. Salary (decimal (integer) / unsigned integer)

Defining a structure in C :

To define structures in C language, first we have to include a keyword struct which is followed by name of variable and declaration  of members.

Syntax :

struct structure_name
{
member definition;
member definition;
};

Members ( i.e. data elements ) of structure can be of any data-type.

For example :

struct employee_details  
{
 char Name_employee[100];
 unsigned int Employee_ID;
 char Designation[50];
 unsigned int Salary ;
};

Calling of the members declared in structure :

For calling or accessing members declared in a structure, you have to use a dot operator(.)

Syntax :

struct structure_name variable_name

For example:

struct employee_details employee_1

In above code we have declare employee_1(i.e. a variable_name used to access the structure employee_details) having a datatype employee_details.

C program to illustrate declaring and accessing structures :

#include<stdio.h>
struct employee_details
{  
  char Name_employee[100];
  unsigned int Employee_ID;
  char Designation[50];
  unsigned int Salary;
};

int main()
{
  struct employee_details employee_1;
  strcpy(employee_1.Name_employee,"Aayushi kumawat");
  employee_1.Employee_ID = 4001;
  strcpy(employee_1.Designation,"Team leader");
  employee_1.Salary = 55000;

  printf("Details of employee 1 are \n");
  printf("Name of the employee..%s \n",employee_1.Name_employee);
  printf("Employee ID...%u\n",employee_1.Employee_ID);
  printf("Designation...%s\n",employee_1.Designation); 
  printf("Salary...%u\n",employee_1.Salary);
}

When above code is compiled and executed successfully,the result given below is obtained.

Output:

Details of employee 1 are
Name of the employee.. Aayushi kumawat
Employee ID...4001
Designation...Team leader
Salary...55000

This is all about basics of structures that are available in C programming language along with syntax of declaring and accessing them to keep a record.

If you have any query please comment below.

Leave a Comment

Your email address will not be published. Required fields are marked *