C LANGUAGE UNIONS | UNIONS IN C | UNION IN C LANGUAGE WITH EXAMPLE | DIFFERENCE BETWEEN STRUCTURE AND UNION | ACCESS UNION MEMBER | C UNIONS |UNIONS IN C PROGRAMMING
A union is a special data type that allows to store different data
types in the same memory location. You can define a union with many
members, but only one member can contain a value at any given time.
Syntax of Union:-
union unionname
{
Union members1;
Union members2;
------------------
};
The union tag is optional and each member definition is a normal variable
definition, such as int i; or float f; or any other valid variable definition. At the
end of the union's definition, before the final semicolon, you can specify one or
more union variables, but it is optional. Here is the way you would define a
union type named Data having three members i, f, and str:
union Data
{
int i;
float f;
char str[20];
} data;
Now, a variable of Data type can store an integer, a floating-point number, or a string of characters. It means a single variable, i.e., same memory location, can be used to store multiple types of data. You can use any built-in or user-defined data types inside a union based on your requirement.
The memory occupied by a union will be large enough to hold the largest
member of the union. For example, in the above example, Data type will occupy 20 bytes of memory space because this is the maximum space which can be occupied by a character string. The following example displays the total memory size occupied by the above union:
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
printf( "Memory size occupied by data : %d\n", sizeof(data));
return 0;
}
When the above code is compiled and executed, it produces the following result:
Memory size occupied by data : 20
To access any member of a union, we use the member access operator (.).
The member access operator is coded as a period between the union variable
name and the union member that we wish to access. You would use the keyword union to define variables of union type. The following example shows how to use unions in a program:
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
data.i = 10;
data.f = 220.5;
strcpy( data.str, "C Programming");
printf( "data.i : %d\n", data.i);
printf( "data.f : %f\n", data.f);
printf( "data.str : %s\n", data.str);
return 0;
}
When the above code is compiled and executed, it produces the following result:
data.i : 1917853763
data.f : 4122360580327794860452759994368.000000
data.str : C Programming
Here, we can see that the values of i and f members of union got corrupted
because the final value assigned to the variable has occupied the memory
location and this is the reason that the value of str member is getting printed
very well.
Now let's look into the same example once again where we will use one variable at a time which is the main purpose of having unions:
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
data.i = 10;
printf( "data.i : %d\n", data.i);
data.f = 220.5;
printf( "data.f : %f\n", data.f);
strcpy( data.str, "C Programming");
printf( "data.str : %s\n", data.str);
return 0;
}
When the above code is compiled and executed, it produces the following result:
data.i : 10
data.f : 220.500000
data.str : C Programming
Here, all the members are getting printed very well because one member is being used at a time.
C LANGUAGE UNIONS | UNIONS IN C | UNION IN C LANGUAGE WITH EXAMPLE | DIFFERENCE BETWEEN STRUCTURE AND UNION | ACCESS UNION MEMBER | C UNIONS |UNIONS IN C PROGRAMMING
A union is a special data type that allows to store different data
types in the same memory location. You can define a union with many
members, but only one member can contain a value at any given time.
Union:-
- C Union is also like structure, i.e. collection of different data types which are grouped together. Each element in a union is called member.
- Union and structure in C are same in concepts, except allocating memory for their members
Union in C | My CS Tutorial |
Difference between Structure and Union (Imp):-
- Structure allocates storage space for all its members separately. Whereas, Union allocates one common storage space for all its members
- We can access only one member of union at a time. We can’t access all member values at the same time in union. But, structure can access all member values at the same time. This is because, Union allocates one common storage space for all its members. Whereas Structure allocates storage space for all its members separately.
The difference between structure and union is:-
- The amount of memory required to store a structure variable is the sum of the size of all the members.On the other hand, in case of unions, the amount of memory required is always equal to that required by its largest member.
- In case of structure, each member have their own memory space but In union, one block is used by all the member of the union.
Syntax of Union:-
union unionname
{
Union members1;
Union members2;
------------------
};
The union tag is optional and each member definition is a normal variable
definition, such as int i; or float f; or any other valid variable definition. At the
end of the union's definition, before the final semicolon, you can specify one or
more union variables, but it is optional. Here is the way you would define a
union type named Data having three members i, f, and str:
union Data
{
int i;
float f;
char str[20];
} data;
Now, a variable of Data type can store an integer, a floating-point number, or a string of characters. It means a single variable, i.e., same memory location, can be used to store multiple types of data. You can use any built-in or user-defined data types inside a union based on your requirement.
The memory occupied by a union will be large enough to hold the largest
member of the union. For example, in the above example, Data type will occupy 20 bytes of memory space because this is the maximum space which can be occupied by a character string. The following example displays the total memory size occupied by the above union:
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
printf( "Memory size occupied by data : %d\n", sizeof(data));
return 0;
}
When the above code is compiled and executed, it produces the following result:
Memory size occupied by data : 20
Accessing Union Members :-
To access any member of a union, we use the member access operator (.).
The member access operator is coded as a period between the union variable
name and the union member that we wish to access. You would use the keyword union to define variables of union type. The following example shows how to use unions in a program:
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
data.i = 10;
data.f = 220.5;
strcpy( data.str, "C Programming");
printf( "data.i : %d\n", data.i);
printf( "data.f : %f\n", data.f);
printf( "data.str : %s\n", data.str);
return 0;
}
When the above code is compiled and executed, it produces the following result:
data.i : 1917853763
data.f : 4122360580327794860452759994368.000000
data.str : C Programming
Here, we can see that the values of i and f members of union got corrupted
because the final value assigned to the variable has occupied the memory
location and this is the reason that the value of str member is getting printed
very well.
Now let's look into the same example once again where we will use one variable at a time which is the main purpose of having unions:
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
data.i = 10;
printf( "data.i : %d\n", data.i);
data.f = 220.5;
printf( "data.f : %f\n", data.f);
strcpy( data.str, "C Programming");
printf( "data.str : %s\n", data.str);
return 0;
}
When the above code is compiled and executed, it produces the following result:
data.i : 10
data.f : 220.500000
data.str : C Programming
Here, all the members are getting printed very well because one member is being used at a time.
C LANGUAGE UNIONS | UNIONS IN C | UNION IN C LANGUAGE WITH EXAMPLE | DIFFERENCE BETWEEN STRUCTURE AND UNION | ACCESS UNION MEMBER | C UNIONS |UNIONS IN C PROGRAMMING
Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this program or want to give any suggestion send email on hc78326@gmail.com
Created by-- HARSH CHAUHAN
No comments:
Post a Comment