All about structure in C language

Structure in C as well as other language is a user defined data type which can store different data type inside it. We have to use struct keyword to declare a structure. Every elements of the structure is called the member.

Structure is used to store different types of data and by this we can store any information of a system very easily. Before going ahead we need to know the basic syntax of a structure. So, lets see the syntax of a structure.

// Syntax of structure in c
struct structName{
  dataType member1;
  dataType member2;
  .......
  .......
  dataType memberN;
};

Now, lets see an example of declaring a structure in C to store the information of some students in C.

// example of declare a structure in c
struct students{
  int rollNo;
  char name[30];
  int age;
  float CGPA;
  char remark[200];
};

When we declare a structure in C, then the allocation of memory is allotted for every variable inside it. Consider the above example. Here, we have declared a structure in C with five variable inside the structure.

Now, if I declare any structure variable like firstStudent, secondStudent, thirdStudent etc then we will get the space of five variable for every student in our memory. For this case we will get the space as like this.

4 bytes for int rollNo.

1 * 30 = 30 bytes for char name[].

4 bytes for int age.

4 bytes for float CGPA.

1 * 200 = 200 bytes for char remark.

So, total memory for a structure variable will be 4 + 30 + 4 + 4 + 200 = 242 bytes.

How to declare structure in C language

After declaring a structure in C we need to declare one or more structure variable to access the member of the structure. There are two way to declare a structure variable in C. These are as follows;

  • By struct keyword inside of main () function
  • declaring at the time of defining the structure

Declaring structure in C by struct keyword

// Declaring structure variable by struct keyword
struct student{    // defining structure
    int roll;
    char name[30];
    float CGPA;
};

int main(){
    struct student s1, s2;   // declaring structure variable
    return 0;
}

Declaring at the time of defining the structure

// Declaring at the time of defining the structure
struct student{
  int roll;
  char name[30];
  float CGPA;
}s1,s2;

Among these two method to declare structure variable we should choose the second method if the number of variable are fixed. Otherwise we should choose the first method as it gives the flexibility to declare structure variable many times.

For first case, it is must to declare the structure variable inside the main() function.

How to access the members of a structure?

We can access the member of a structure variable using the following two methods.

  • By dot (.) operator
  • By structure pointer (->) operator

Among these two methods dot (.) operator is being used mostly among programmers. So, we will see some C program using this dot (.) operator here. You can also use structure pointer operator.

Example C program using structure in C

Here we will see an example program using structure in C to add two distance. Let’s try to understand this.

// Example C program to add two distances
#include <stdio.h>
struct add_distance{
  int feet;
  float inch;
} first_distance, second_distance, sum;

int main(){
  printf("For first distance\n");

  printf("Enter feet here : ");
  scanf("%d", &first_distance.feet);
  printf("Enter inch here : ");
  scanf("%f", &first_distance.inch);

  printf("\nFor second distance\n");

  printf("Enter feet here : ");
  scanf("%d", &second_distance.feet);
  printf("Enter inch here : ");
  scanf("%f", &second_distance.inch);

  sum.feet = first_distance.feet + second_distance.feet;  // add feet

  sum.inch = first_distance.inch + second_distance.inch;  // add inch

  while(sum.inch >= 12){     // as 12 inch = 1 feet
    ++sum.feet;
    sum.inch = sum.inch - 12;
  }

  printf("\nSum of two distances is = %d feet %.f inch\n", sum.feet, sum.inch);
  return 0;
}

Output of adding distance structure program:

Structure in C, add two distance program in c using structure

C program to store employees information using structure

In this C program we will store and print some employees information using structure in C. Let’s see the program.

// C program to store employees information using structure
#include<stdio.h>
#include <string.h>
struct struct_of_employee{
  int id;
  char name[60];
  float salary;
}employee1,employee2;

int main(){

  employee1.id = 301;
  strcpy(employee1.name, "John Doe");
  employee1.salary = 43258.80;

  employee2.id = 302;
  strcpy(employee2.name, "Michel Stephen");
  employee2.salary = 52542.50;

  printf( "ID of employee one is = %d\n", employee1.id);
  printf( "Name of employee one is = %s\n", employee1.name);
  printf( "Salary of employee one is = %.2f\n", employee1.salary);

  printf( "\nID of employee two is = %d\n", employee2.id);
  printf( "Name of employee two is = %s\n", employee2.name);
  printf( "Salary of employee two is = %.2f\n", employee2.salary);

  return 0;
}

When you compile and run this program then you will see the following output.

Output of this program

structure in c, c structure, store and print employees information using c structure