Different between Structure and union in C

Different between Structure and union in C

 Different between Structure and union

 Structure and union both are collections of heterogeneous data items treated as a single unit. But there is some difference which is highlights below.

Structure

Union

Structure is a collection of data items having dissimilar data type.

Union is the same as structure but differs in its storage class.

Memory size in structure determined by the sum of memories allocated to all the members.

 

Memory size is determined by the memory allocated to the largest member of union.

The keyword struct is used to define structure.        

 

Keyword union is used to define structure.

Multiple members can be simultaneously accessed at a time

 

Only one member can be accessed at a time.

Syntax

struct structure name

{
    member 1;

    member 2;

    ................;

   member n;

};

struct structure name var 1, var2..var n;

 

Syntax

union union name

{
    member 1;

    member 2;

    ................;

   member n;

};

union union name var 1, var2..var n;

Example

struct record;

{

    int roll;

    float mark;

    char name[20];

};

struct record a;


 Example

struct record;

{

    int roll;

    float mark;

    char name[20];

};

struct record a;

Program

Write a C program to take input Roll no

and print using structure

#include<stdio.h>
#include<conio.h>
void main()

{
   struct record

     {

       int roll;

       name[20];

      };

struct record a;

printf("\n Enter Roll no and Name of

              a student");

scanf("%d%s", &a. roll, a.mark);

printf("\n Roll no = %d\tMark=%s",

          a.roll, a. mark):

getch();

}


Program

Write a C program to take input Roll no

and print using union

#include<stdio.h>
#include<conio.h>
void main()

{
   union record

     {

       int roll;

       name[20];

      };

union record a;

printf("\n Enter Roll no of a student");

scanf("%d"  &a.mark);

printf("\n Enter Name of a student");

scanf("%s" a.mark);

printf("\n Roll no = %d", a.roll);

printf("\n Name= %s", a.name);      

getch();

}



Introduction Structure

Structure is a collection of heterogeneous data items treated as a single unit. These data items can be of different types. Each data item is called a member of structure. The keyword struct is used to declare structure. The memory of a structure is determined by the sum of memory allocated to all the members of a structure. In this structure, multiple members can be simultaneously accessed at a time.

Syntax of structure

struct structure_ name

{

data type variable 1;

data type variable 2;

……………………………..;

data type variable n;

};

struct structure_name var1, var 2……….var n;

Example of structure

struct record

{
int roll_no;

char fname[30];

char lname[30];

};

struct record a;  

Program using structure

  #include<stdio.h>

#include<conio.h>

void main()

{

                struct record

                {

                                int roll;

                                float mark;

                                char name[20];

                };

                struct record a;

                {

                                printf("\n Enter Roll number, Name and marks of student");

                                scanf("%d%s%f", &a.roll, a.name, &a.mark);

                    printf("\n Roll number=%d\tName=%s\t marks=%f",a.roll, a.name, a.mark);

                }

                getch();

}

 

The memory size of this program is

Memory allocated by roll variable is 2 bytes (size of integer is 2 bytes)

Memory allocated by mark variable is 4 bytes (size of float variable is 4 bytes)

Memory allocated by name is 20 bytes

The total size of this program is

 

Structure size

Introduction Union

Union is similar to structure but it differs only in its storage location. The union keyword is used to define union. In structure, each member has its own memory block whereas all members of union can share the same memory location. Therefore, union can take less amount of memory than structure and it can access only one member at a time

Syntax of union

Union union_name

{
        data_type member 1;

        data type member 2;

        …………………………….

       data type member n;

};

union_name variable 1, variable 2, variable n;

Program using union

  #include<stdio.h>

#include<conio.h>

void main()

{

                union record

                {

                                int roll;

                                float mark;

                                char name[20];

                };

                union record a;

                {

                                printf("\n Enter Roll number of a student");

                               scanf("%d", &a.roll);

                                printf("\n Enter Name of a student");

                               scanf("%c", a.roll

                                                    printf("\n Enter marks of a student");

                               scanf("%f", &a.roll);

printf("\n Roll number=%d\tName=%s\t marks=%f",a.roll, a.name, a.mark);

getch();

}

 

Size of this program

Memory allocated by markvariable is 20 bytes (mark variable is the largest variable)

 

Union memory size

Memory size structure vs union in the same program

union and structure





2.      


Post a Comment

0 Comments