The perfect place for easy learning...

C Programming Language

×

Topics List

Place your ad here

Structures in C

In C programming language, a structure is a collection of elements of the different data type. The structure is used to create user-defined data type in the C programming language. As the structure used to create a user-defined data type, the structure is also said to be “user-defined data type in C”.
In other words, a structure is a collection of non-homogeneous elements. Using structure we can define new data types called user-defined data types that holds multiple values of the different data type. The formal definition of structure is as follows...

Structure is a colloction of different type of elements under a single name that acts as user defined data type in C.

Generally, structures are used to define a record in the c programming language. Structures allow us to combine elements of a different data type into a group. The elements that are defined in a structure are called members of structure.

How to create structure?

To create structure in c, we use the keyword called "struct". We use the following syntax to create structures in c programming language.

struct <structure_name>
{
      data_type member1;
      data_type member2, member3;
      .
      .
} ;

Following is the example of creating a structure called Student which is used to hold student record.

Creating structure in C

struct Student
{
    char stud_name[30];
    int roll_number;
    float percentage;
} ;


Importent Points to be Remembered
  Every structure must terminated with semicolon symbol (;).
  "struct" is a keyword, it must be used in lowercase letters only.


Creating and Using structure variables

In a c programming language, there are two ways to create structure variables. We can create structure variable while defining the structure and we can also create after terminating structure using struct keyword.
To access members of a structure using structure variable, we use dot (.) operator. Consider the following example code...

Creating and Using structure variables in C

struct Student
{
    char stud_name[30];
    int roll_number;
    float percentage;
} stud_1 ;                  // while defining structure

void main(){
    struct Student stud_2;  // using struct keyword
    
    printf("Enter details of stud_1 : \n");
    printf("Name : ");
    scanf("%s", stud_1.stud_name);
    printf("Roll Number : ");
    scanf("%d", &stud_1.roll_number);
    printf("Percentage : ");
    scanf("%f", &stud_1.percentage);
    
    printf("***** Student 1 Details *****\n);
    printf("Name of the Student : %s\n", stud_1.stud_name);
    printf("Roll Number of the Student : %i\n", stud_1.roll_number);
    printf("Percentage of the Student : %f\n", stud_1.percentage);
}

In the above example program, the stucture variable "stud_1 is created while defining the structure and the variable "stud_2 is careted using struct keyword. Whenever we access the members of a structure we use the dot (.) operator.

Memory allocation of Structure

When the structures are used in the c programming language, the memory does not allocate on defining a structure. The memory is allocated when we create the variable of a particular structure. As long as the variable of a structure is created no memory is allocated. The size of memory allocated is equal to the sum of memory required by individual members of that structure. In the above example program, the variables stud_1 and stud_2 are allocated with 36 bytes of memory each.


Importent Points to be Remembered
  All the members of a structure can be used simultaneously.
  Until variable of a structure is created no memory is allocated.
  The memory required by a structure variable is sum of the memory required by individual members of that structure.



Place your ad here
Place your ad here