Structure and Union in C

Structure and Union in C

In this tutorial, You will be knowing about structure and union in C. Before procuring information about structures and unions, you must know all about the array data type and how it works and some examples. 

Also Read: Arrays in C with examples

Structure (struct) in C

Structure is a user-defined data type in C. It merges the data elements of different kinds together under a single name. You will need to define the data types and their identifiers before creating a structure.

The structure is a composite data type of C language because the variable of structure can hold many values of different data types. 

These data members can be of different data values. Each and every variable of a structure is allocated with multiple blocks of memory of different sizes.

These blocks can be accessed by using the dot operator with the variable name. You can create a structure within any structure in C programming which is often called nested structures.


How to define structures in C?

Before making a variable, a structure needs to be created first. This structure is created by using keyword STRUCT. 

In this declaration, apart from the name of the data type, the data members are also mentioned. 

Syntax of struct

struct structure_Name 
{
    data_Type member_name1;
    data_Type member_name2;
    data_Type member_name3;
    ...
};

Example: structure in c

struct Student 
{
    char name[20];
    int roll_no;
    int age;
};

Here, a derived data type (struct data type) is defined and named as Student. Now, you can create the variables of that type.


When a structure is defined, no memory is allocated. The memory or space is only allocated when the variables of the defined structure type are created.


How to create structure (struct) variables?

struct Student 
{
    char name[20];
    int roll_no;
    int age;
};

int main()
{
   struct Student name1, name2, name3;
   return 0;
}
You can also create the variables of defined structure type by writing the code as given below.

struct Student 
{
    char name[20];
    int roll_no;
    int age;
} name1, name2, name3;
Here, three variables name1 name2 , and name3 of type  struct Student are created.


How to access the structure variables (members)?

You can access the variables or members of the struct type by making use of two operators.
  1. Member operator - (.) 
  2. Structure pointer operator - (->)
If you wanna access the name of Student struct type then you can do this by using dot operator as

Student.name

Example: structure in c

#include<stdio.h>
struct Student 
{
    char name[20];
    int roll_no;
    int age;
};

int main()
{
   struct Student name1;
   
   printf("Enter Name \n");
   scanf("%s", &name1.name);

   printf("Enter Age \n");
   scanf("%d", &name1.age);

   printf("Enter Roll Number \n");
   scanf("%d", &name1.roll_no);

   printf("Name: %s \n", name1.name);
   printf("Age: %d \n", name1.age);
   printf("Roll Number: %d \n", name1.roll_no);
}

Output


Enter Name 
Shruti 
Enter Age 
18
Enter Roll Number 
101
Name: Shruti 
Age: 18 
Roll Number: 101 

Example: structure in c

#include<stdio.h>
struct Example
{
    int number;
    char character;
    float decimal;
};

int main()
{
   struct Example num1[5];
   int i;

   for(i=0 ; i<5; i++)
   {
      printf("\n Enter integer number \n");
      scanf("%d", &num1[i].number);

      printf("Enter any character \n");
      scanf("%c", &num1[i].character);

      printf("Enter decimal number \n");
      scanf("%f", &num1[i].decimal);
   }

   for(i=0 ; i<5; i++)
   {
      printf("\n Given Integer number is %d \n", num1[i].number);
      printf("Given Character is %c \n", num1[i].character);
      printf("Given Decimal number is %f \n", num1[i].decimal);
   }
}

Output

Enter integer number 
5
Enter any character 
k
Enter decimal number 
5.0

Enter integer number 
51
Enter any character 
n
Enter decimal number 
3.5

Enter integer number 
62
Enter any character 
s
Enter decimal number 
35.23

Enter integer number 
31
Enter any character 
v
Enter decimal number 
32.15

Enter integer number 
51
Enter any character 
x
Enter decimal number 
5.5

Given Integer number is 5 
Given Character is k 
Given Decimal number is 5.0 

Given Integer number is 51 
Given Character is n 
Given Decimal number is 3.5 

Given Integer number is 62 
Given Character is s 
Given Decimal number is 35.23 

Given Integer number is 31 
Given Character is v 
Given Decimal number is 32.15 

Given Integer number is 51 
Given Character is x 
Given Decimal number is 5.5

Union in C

Union is a special type of user-defined data type that allows us to store different data types within the same location memory. 

It is much more similar to structure except the memory it has been allocated is different from the memory allocation of structure.

How to define union in C?


Before making a variable, a union needs to be created first. This union is created by using the keyword UNION. 


In this declaration, apart from the name of the data type, the data members are also mentioned. 

Syntax of union

union Union_Name 
{
    data_Type member_name1;
    data_Type member_name2;
    data_Type member_name3;
    ...
}; 



Example: union in c


union Student 
{
    char name[20];
    int roll_no;
    int age;
};

Here, a derived data type (union data type) is defined and named as Student. Now, you can create the variables of that type.


How to create union variables?


union Student 
{
    char name[20];
    int roll_no;
    int age;
};

int main()
{
   union Student name1, name2, name3;
   return 0;
}



How to access the union variables (members)?

You can access the variables or members of the union type by making use of the (.member access operator. Let see the examples of accessing the union variables.

#include<stdio.h>
#include<string.h>
union AnyName
{
   int i;
   float f;
   char str[20];
};
 
int main( )
{
   union AnyName example;        

   example.i = 10;
   example.f = 220.5;
   strcpy(example.str, "Learning Mania");

   printf("example.i : %d \n", example.i);
   printf("example.f : %f \n", example.f);
   printf("example.str : %s \n", example.str);
}

Output

example.i : 1918985548 
example.f : 4464421607515930630576467345408.000000 
example.str : Learning Mania

Example: union in c

#include<stdio.h>
union NewData
{
   int integer_number;
   float float_number;
   char str[20];
};
 
int main( )
{
   union NewData Data;        

   Data.integer_number = 101;
   Data.float_number = 211.6;
   strcpy(Data.str, "Learning Mania");

   for(int i=0; i<5; i++)
   {
      printf("\n Data.integer_number : %d \n", Data.integer_number);
      printf("Data.float_number : %f \n", Data.float_number);
      printf("Data.str : %s \n", Data.str);
   }
}

Output

Data.integer_number : 1918985548 
Data.float_number : 4464421607515930630576467345408.000000 
Data.str : Learning Mania 

Data.integer_number : 1918985548 
Data.float_number : 4464421607515930630576467345408.000000 
Data.str : Learning Mania 

Data.integer_number : 1918985548 
Data.float_number : 4464421607515930630576467345408.000000 
Data.str : Learning Mania 

Data.integer_number : 1918985548 
Data.float_number : 4464421607515930630576467345408.000000 
Data.str : Learning Mania 

Data.integer_number : 1918985548 
Data.float_number : 4464421607515930630576467345408.000000 
Data.str : Learning Mania 

Difference Between Structure and Pointer in C

In case of the structure, each data member has its own memory space and that is why we can give all the values for all the members at a time but in case of union, only one block of memory is allocated whose size is equal to the size of the data members greatest among all. 

This saves memory but all the data members share the single block of memory. So, only one value of a data member can present at a time.

Read More about the differences >> Tutorial Gateway


<< Previous                                                                                  Next >>

Newest
Previous
Next Post »

Comment Below: ConversionConversion EmoticonEmoticon