Saturday, 16 March 2019

LEARN UNION IN C : WITH VIDEO


Union in C


Union in C are exactly like the structure in C. Just like structure, the union is a user-defined data type. Syntax to define a union in C is similar to structure. There is only one difference in the structure and union that is the storage of memory. Each member has its own storage location in the structure, while the members of the union shared the same memory location, which size is equal to the largest data member. 

Union in C
Storage difference between structure and union in C


The syntax of Union in C


union name_of_union
{
data-type member1;
data-type member2;
data-type member3;
...............................
...............................
...............................
data-type member N;
};

The syntax of the union is similar to the syntax of structure in C. There is only one difference in the syntax of structure and union is only union keyword is used in place of struct keyword. 

Example
union data
{
int a;
int b;
};


Declaration of the union variable

Union variable can be declared as:

1. Union variable along with union definition

Union variable can be declared before the ending of union definition.

Syntax
union name_of_union
{
data-type member1;
data-type member2;
data-type member3;
..............................
..............................
..............................
data-type member N;
}union_variable;


Example:
union data;
{
int a;
int b;
}add;

2. Union variable after structure definition

Union variable can be declared anywhere in the program after the union definition based on the union scope.

Syntax
union name_of_union
{
data-type member1;
data-type member2;
data-type member3;
..............................
..............................
.............................
data-type member N;
};

main ( )
{
union name_of_union union variable;
}


Example
union data
{
int a;
int b;
}
main ( )
{
union data w,x,y;
}

Accessing a member of the union in C

Union members are also accessed as members of the structure using the dot operator(.).

Syntax
Union_variable.name_of member;

Program
#include <stdio.h>
//Declaration of union
union data
{
    int a;
    char b;
};

int main ( )
{
//A union variable q
    union data q;
    q.a = 132; // q.a gets value 132
    q.b = 'A'; //q.b gets value 'A'
    printf ("%d\n", q.a);
    printf ("%c\n", q.b);
    return 0;
}

Output
Union in C









As we can see that the value of 'a' does not occur as mention in the program while the value of  'b' is correct in our output. This is because of all the variables share the same memory. Hence, the only member whose value is currently stored will have the memory.

Pointer to unions

Union has pointers just like structures and with the help of arrow operator (->), we can access the members. 


C program demonstrates the working of pointers to the union.

#include <stdio.h>
union data
{
    int a;
    char b;
};
int main ( )
{
    union data q;
    q.a = 66;
    //q1 is a pointer to union q
    union data* q1=  &q;
    //Accessing union member using pointers
    printf ("%d %c", q1->a, q1->b);
    return 0;
}

Output
Union in C




The advantage of the union in C

Some programmers do not use the union properly. The main issue of the union is to save memory because the union members share the same memory.

Anonyms union and structure in C

The union and structure that do not have the name then they are called anonyms union/structure. The definition is similar to normal union or structure the only difference is defined without name or tag.

//Anonyms union example
union
{
int a;
char b;
};

//Anonyms structure example
struct
{
int a;
char b;
};

Because there are no variables and names, we access directly members. This accessibility works only inside the scope where the anonyms union is defined.

#include<stdio.h>
struct Scope
{
    // Anonymous union
    union
    {
        char a;
        int b;
    };
};

int main()
{
    struct Scope w;
    w.b = 66;

    //  members of union can accessed directly
    printf("w.a = %c, w.b = %d", w.a, w.b);

    return 0;
}


Output
Union in C







//C program demonstrate working of anonyms structure
#include<stdio.h>
struct Scope
{
    // Anonymous union
    struct
    {
        char a;
        int b;
    };
};

int main( )
{
    struct Scope w;
    w.a = 'B';
    w.b = 66;

    //  members of structure can accessed directly
    printf("w.a = %c, w.b = %d", w.a, w.b);

    return 0;
}


Output
Union in C




Difference between structure and union in C

1. Keyword: The keyword struct is used to define the structure in C and union keyword is to define union in C.
2. Size: Size of memory is allocated for each member define in structure but in the union, memory is allocated as per the largest member.
3. Memory: In structure, each member has its own space but in the union in C, all member shared the same memory. 
4. Altering value: Change in the value of the member will not affect the other members in structure but union in C it affects.
5. Accessing of members: Members of structure can be accessed at the same time but in the union, only one member can be accessed at the same time.
























































No comments:

Post a Comment