C Structures

Let us understand Structures in C programming. In this lesson, we will cover the following concepts related to structures:

  • What are Structures
  • Define a Structure
  • Declare a Structure in two ways
  • Access Structure Members
  • Example of Structures covering the above concepts

Before moving further, we’ve prepared a video tutorial to understand what structures are in C:

Video: Structures in C

Video: Structures in C (Hindi)

What are Structures

Structures in C allow you to store data of different types. Easily store variables of types, such as int, float, char, etc. For example, storing the details of employees, with the following members:

  • name: string type
  • age: int type
  • zone: string type
  • salary: float type

You might wonder about the difference between structures and arrays. Well, Arrays store data of similar types, such as an array of ints, whereas Structures store data of different types.

Define a Structure

Let us understand the above concept with an example to define a structure. To define a structure, use the struct keyword:

struct emp 
{   
    char name[15]; 
    int age;
    char zone[10];	
    float salary;  
};

Above,

  • The name of the structure is emp,
  • The structure members or fields are name, age, zone, and

Declare a Structure

We defined the structure above. Let us now see how to declare a structure using two ways:

  1. Declare a structure while defining it.
  2. Declare a structure with struct

1. Declare a structure while defining it

struct emp 
{   
    char name[15]; 
    int age;
    char zone[10];	
    float salary;  
};  e1, e2, e3;

The e1, e2, and e3 above are variables. We can use them to access the values.

2. Declare a structure with struct

We can also declare the variables e1, e2, e3 using the struct keyword. First, define it:

struct emp 
{   
    char name[15]; 
    int age;
    char zone[10];	
    float salary;  
};

Declare e1, e2, and e3 of type emp:

struct emp e1, e2, e3;

Access Structure Members

To access a structure member in C language, use a period i.e. the member access operator. Following is the syntax:

structure_variable.structure_member

For example, access the structure members for variable e1 i.e. the 1st employee:

e1.name
e1.age
e1.zone
e1.salary

C Structures – Example

Let us see an example of creating a structure in C. We will define and declare a structure, and also learn to access structure members in C:

#include <stdio.h>
#include <string.h>
 
struct Emp 
{   
    char name[15]; 
    int age;
    char zone[10];	
    float salary;  
};  
 
int main( ) {

   // Declare e1, e2, e3 of type Emp
   struct Emp e1;    
   struct Emp e2;    
   struct Emp e3;    
   
   // Set the Employee1 information 
   strcpy(e1.name, "John");
   e1.age = 25;    
   strcpy(e1.zone, "North");
   e1.salary=30000;    
 
   // Display Employee1 data
   printf("Employee1 1 name = %s\n", e1.name);
   printf("Employee1 1 age = %d\n", e1.age);
   printf("Employee1 1 zone = %s\n", e1.zone);
   printf("Employee1 1 salary = %f\n", e1.salary);

   return 0;
}

Output

Employee1 1 name = John
Employee1 1 age = 25
Employee1 1 zone = North
Employee1 1 salary = 30000.000000

In the above example, we have used strcpy. In C, strings can’t be directly assigned using the operator because arrays, including character arrays, don’t support direct assignment. Therefore, strcpy was used to copy the contents of one string to another.

Structures example without strcpy

Let us see another example without using strcpy. For that, initialize the structure at the time of declaration:

struct Emp e1 = {"David", 30, "East", 40000.5};

Here is the same example without using strcpy:

// Structures example in C
#include<stdio.h>

struct Emp {
    char name[15];
    int age;
    char zone[10];
    float salary;
};

int main() {
   
   struct Emp e1 = {"David", 30, "East", 40000.5};

   // Display Employee1 data
   printf("Employee Name = %s\n", e1.name);
   printf("Employee Age = %d\n", e1.age);
   printf("Employee Zone = %s\n", e1.zone);
   printf("Employee Salary = %.2f\n", e1.salary);

   return 0;

}

Output

Employee Name = David
Employee Age = 30
Employee Zone = East
Employee Salary = 40000.50

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:


For Videos, Join Our YouTube Channel: Join Now


Read More:

C Recursion
C - Math Functions
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment