LEARN UNION IN C : WITH VIDEO


Union in C


Union in C is identical to the structure in C. Just like structure, the union is a user-defined data type. Syntax to define a union is similar to the structure. There is only one difference in the structure and union that is the storage of memory. Each member has its 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


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. The only difference is that the union keyword is used instead of the struct keyword. 

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


Declaration of the union variable

Two ways to declare the union variable in C, 

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

Accessing of a union member is similar to the accessing of structure. You can obtain a union member with the help of 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
65
A

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 union in C

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
66 B

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. Its definition is similar to the definition of union and structure. The only difference is that it defines 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 members directly. 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: In the structure, the size of memory is allocated for each member, 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, all member shared the same memory space. 
4. Altering value: Change in the value of the member will not affect the value of other members in structure but union, 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.




LEARN UNION IN C : WITH VIDEO LEARN UNION IN C : WITH VIDEO Reviewed by Glory on 12:24 Rating: 5

No comments:

Powered by Blogger.