Structures in C Programming

Structures in C : In C Programming, Arrays are helpful to store a group of similar data type elements. But, there are some situations where we have to group non-similar data types (int, float, char, etc.). To handle these type situations C programming introduced the concept of Structures.

Struct keyword is used to create structures in C programming. Structures in C are used to group different data types to organize the data in a structural way.

For example, we are storing employee details such as name, id, age, address, and salary. From the names, you can understand that they are not the same data type. Normally we create a separate variable for the name, id, address, age, and salary, but how about storing the same for 5 employees? It will be difficult for the developer to assign the variable names to each. So, We create a C structure using struct Keyword and assign the name as an employee.

Structures in C Syntax

The basic syntax of the C structures using struct is as shown below

struct Structure_Name
{
  Data_Type Variable_Name;

  Data_Type Variable_Name;
 
  ………….
};

The items available in the structures in C Programming

  • The struct is the system reserved keyword used to create and access structures.
  • Structure_Name: Name you desire to give. For ex, employees, person, students
  • Data_Type: Data type of the declared variable. For example, int, float, char.
  • Variable_Name – For example, id, name, age, salary.

struct Example

struct Employee // Name = Employee

{
  int age; //Data_Type = int; Variable Name = age

  char name[50]; //Data Type = char Array; Variable_Name = name

  float salary; //Data Type = float; Variable Name = salary
};

Declaration of Structures in C Programming

Members inside the C structures will not store any memory location until they are associated with variables. So, we have to create the struct variable before using it. We can declare the struct variables in multiple ways

First Approach

Declaring the structure first and then in the main function creating the struct variable

struct Employee
{
  int age;

  char name[50];

  float salary;
};

//Creating the Struct variable in main() function
struct Employee emp1, emp2;

emp1 = {25, “Dave”, 25000};

emp1and emp2 = variables of struct data type.

Second Approach

We are creating the struct variables at the time of the C structure declaration.

struct Employee
{
  int age;

  char name[50];

  float salary;

} emp1, emp2;

Accessing Members

We can locate the structure members using the dot (.) operator or members operator. Let us use the above struct example.

//Assigning the values

 emp2.age = 26;

 strcpy (emp2.name, “Michel”);

 emp2.salary = 46000.50;

Structures in C Programming Example

In this example program, we declare the structure with 5 data members. Next, we display each item as output.

#include <stdio.h> 
#include <string.h> 

struct Employee 
{
  int Empolyee_ID;
  int age;  
  char Name[50];
  char Department[20];
  float Salary;
};

int main() 
{
  struct Employee emp1 = { 101, 25, "Dave", "IT", 25000.50 };
  struct Employee emp2;
  
     emp2.Empolyee_ID = 102;
     emp2.age = 28;
     strcpy(emp2.Name, "Christofer");
     strcpy(emp2.Department, "Science");
     emp2.Salary = 32000.70;

  printf(" Details of the Employee1 \n " );
  printf(" Employee Id = %d \n ", emp1.Empolyee_ID );
  printf(" Employee Age = %d \n ", emp1.age );
  printf(" Employee Name = %s \n ", emp1.Name );
  printf(" Employee Department = %s \n ", emp1.Department );
  printf(" Employee Salary = %.2f \n\n ", emp1.Salary );

  printf(" Details of the Employee1 \n " );
  printf(" Employee Id = %d \n ", emp2.Empolyee_ID );
  printf(" Employee Age = %d \n ", emp2.age );
  printf(" Employee Name = %s \n ", emp2.Name );
  printf(" Employee Department = %s \n ", emp2.Department );
  printf(" Employee Salary = %.2f \n ", emp2.Salary );

  return 0;
}
Structures in C Programming

In this C structures example, we declared an Employee with Empolyee_ID, age, Name, Department, and Salary members with appropriate Data Types. Please refer to Arrays article in C Programming

Within the main() function, We created Employee struct variable emp1 and assigned the values to them.

struct Employee emp1 = {101, 25, "Dave", "I T", 25000.50};

It means the values for emp1 struct are as follows

Empolyee_ID = 103

age = 25

Name = Dave

Department = I T

Salary = 25000.50

We created one more Employee C structure variable, emp2. This time, we used the member’s or dot operator (.) to specify the values based on the data type. The printf statements are to output each member present in the struct variables.

NOTE: Using the member’s operator, we can assign values to the int and float variables. However, for strings, we have to use the string strcpy() function only.