Unions
- Why Unions?
- As you know 'C' language provide us with variable to store the single data ,Array for storing related type of data,then Why Union ?
- Because, In real life there is a situation where we have store a data which is logical related to each other but they are not of same type.
- They are related but ,Data contains Dissimilar information,which means each data is of different datatype.
- For example you have to store a student information:
then attributes of student will be Name ,roll_no,class,div,address,etc then how can you store this data by Using int ,char or by using simple array,So this not possible by predefined datatypes. - Now , at this type of situation Union comes in action.
What are Unions?
- Unions are Programmer define Data types.
- Unions is a collection of logically related data of different data types which are grouped together under a single name.
- Unions are same like Structures , Now if you dont know what are structures than you can go through this topic: What are Structures?.
- But, It is not necessary, that for learning Unions You should know what are structures.
- Unions are just an another way of defining programmer defined data types.
- There is slightly difference between structures and Unions and that difference is about storage.
- In Structures each member stores the unique storage area in the computer memory.
- But , all members of union uses a single shared memory location which is equal to the size of its largest data member
- Look the below image to understand the concept:
- As each member shares the same single memory, that is why only one members can be active at a time.
- Also , you cannot Initialize the Union Variables.
- You can use them when values of multiple members need not to be assigned to all the members at any one time.
-
Syntax: defining Unions.
union union_name { datatype member1; // each item in union is member datatype member2; ...... ...... };
Note: union is a keyword.
while defining union you have to use that keyword. - Let's define our first union.
We will define a Union for storing data of student.
Definition:
union student //union name { int rollno; //member1 ,integer variable which will store, //student roll no. char name[20]; //member2 a character array of 20 character //which will store name of student. char div; //member3 a character variable, //which will store division of student. };
In Union student , each member will share 20 bytes ,because it is the size of largest data of member name.
- Q:Define a union employee and it should have employee name,employee id,employee designation,and employee salary as it's members.
Definition:
union employee //union name { int employee_id; //member1 int variable which, //will store employee id char employee_name[20]; //member2, character array which will, //store name of employee. char employee_desig[15]; //member3,character array which will, //store designation of employee. int employee_salary; //member4, int variable which will, //store employee salary. };
In Union employee , each member will share 20 bytes ,because it is the size of largest data of member employee_name
- Q:Define a union Book and it should have book name,book id ,book price,and author name as an member in it.
Definition:
union Book //union name { int Book_id; //member1 int variable which, //will store Book id char Book_name[20];//member2, character array which will, //store name of book. char author_name[30];//member3,character array which will, //store name of author. int Book_price; //member4, int variable which will, //store price of book. };
In Union Book , each member will share 30 bytes ,because it is the size of largest data of member author_name.
- The union template ends with the semicolon ';'.
- The members of union are enclosed in parenthesis {}.
- Now we have learned how to define a union ,so let's use it.
- For using it , we have to declare the union.
- let's see syntax of declaring union variable
- union keyword is used while declaring structure variable.
- Above we have defined student data type,so declare a variable of student data type.
Declaration:
void main() { union student s1; //s1 is a variable of student type. //as student is a datatype which is, //define by us. }
- Q:declare a variable 'emp1' of employee datatype.
employee datatype has define above.Declaration:
void main() { union employee emp1; //emp1 is a variable of employee type. //as employee is a datatype which is, //define by us. }
- Q:declare a variable 'b1' of Book datatype.
Book datatype has define above.Declaration:
void main() { union Book b1; //b1 is a variable of Book data type. //as Book is a datatype which is, //define by us. }
Syntax:
union union_name variable_name;
union union_name variable_name;
Related Concept:
- How to initialize an structure variable?
- How to Access Structure member?
- What are Nested Structure?
- How to built an Nested Structure?
- How to Access members of Nested Structure?
- How to initialize Nested structure?
- Functions and Structure
- How to pass a structure variable to function?
- How to make function which will take structure variable as arguments?
- How to make function which will return structure variable?
- Array and Structure
- How to make array of structure variable?
- How to initialize array structure variable?
- How to Access members of an array of structure?
- How to take members of array structure as input from the user?
- Pointer and Structure
- How to Point a structure variable?
- How to declare a pointer of structure variable?
- How to Initialize an pointer of structure variable?
- How to access member of structure variable which is point by an pointer?
- What is '->' Operator?
- How to allocate memory for structure variable Dynamically
- How to allocate memory of structure variable using malloc() and calloc() function
- Self Referential Block
- What is Referential Structure?
- How to Create self Referential Structure?
- Applications of Self Referential Structures
- Practice Programs
People Also Searched:
- What are structures?
- Why structures?
- Need of Structure
- Concept of structure
- Array and Pointer
- What is array of pointer?
- how to initialize an array of pointers?
- How to Access elements of an Array Of Pointers?
- Functions and Pointers
- How to pass a Pointer To a Function?
- Call By reference
- Functions In C
- Why Functions?
- What are Functions?
- how to write our functions?
- What are programmer define functions?
- How to declare function?
- How to define a function?
- What is function Prototype?
- how to declare a function?
- what are Actual Parameters?
- What are Formal Parameters?
- Function Call?
- how to call a Function?
- What are different types of calling function?
- Call by value
- call by reference
- Recursion
- Array and Functions
- How to pass an array to a function?
- What is Character array
- Functions for Characters
- Functions from #include<ctype.h> Header File
- what are isdigit(), islower() , isupper(),etc
- what are various string handling functions
- Functions from #include<string.h> Header File
- what are strcpy(), strcmp(), strncmp(), strcmpi(),etc
- strcmp() VS strncmp() VS strcmpi()
- Input And Output Functions
- How to take strings As Input
- What is gets() functions
- What is puts() function
0 Comments
Post a Comment