C – Unions – GENCOR

C – Unions

A union is a special data type available in C 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. Unions provide an efficient way of using the same memory location for multiple-purpose.

Defining a Union

To define a union, you must use the union statement in the same way as you did while defining a structure. The union statement defines a new data type with more than one member for your program. The format of the union statement is as follows −

union [union tag] {
   member definition;
   member definition;
   ...
   member definition;
} [one or more union variables];

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.

I am happy to be here in Gencor which provided me with technical professional and skillful aspects of auto cad, thank you.

Kush Prakash - West Bengal University of Technology

I completed CCNA traning and certification preparation before appearing for ccna global examination 200-120 from gencor.

Amit Kumar - Lovely Professional University

The trainer has good knowledge of the subject.

Saurabh Kumar Suman - Shaheed Bhagat Singh State Technical Campus, Ferozepur

This institute is the best institute in web designing. Trainer sir is good in knowledge and best trained and  provided web designing knowledge to me.

Abhishek Kumar - Guru Gobind Singh Polytechnic College, Talwandi Sabo, (Bhathinda) Punjab

The trainer is good, so I think everything is good. 

Rabindra Kumar Yadav - Samalkha Group of Institutions

The Gencor classes is good for getting the knowledge. The trainer has good communication with students. I completed CAD training in Mechanical from here.

Akash Kumar - Jawaharlal Nehru Technological University, Hyderabad

I gained a lot here in the Gencor and I believe that it will help to enhance my knowledge and personalty further in my life.

Prem Kumar - BIT Sindri

Institute Completed my training as per  Syllabus provided on time. 

Akshay Kumar - The Institution of Civil Engineers, Delhi

Gencor institute is the best institute which completed my course on Civil CAD on time.

Amit Kumar - Reg No - 001/15348 - Civil CAD

The overall system of teaching is favorable and good as I expected and helped a lot in increasing my knowledge.

Sunny Kumar

I appreciate the course by the institute and efforts put in to complete it within the prescribed time limits.

Mohammad Asad Eqbal

The trainer has good knowledge of autocad, so I am happy to complete the training of AutoCAD from here.

Hemant Raj - Reg No.: 001/15307 - Civil CAD

The Trainer is nice one. He teaches well. The institute is good. I have faith on the Institute.

Vikash Kr Mandal - Reg No.: 001/15308 - Mechanical CAD

The institute is very good one and object oriented, focused on setting  up career for engineering students. The trainer is having enough knowledge to guide students.Teaching method is very much appreciated and interactive.

Indranil Mazumdar - Reg No.: 001/15312 - Civil CAD

All class I did here was very good. Faculty provide every detail about syllabus & Course. I am thankful of you.

Sumank Saurav - Reg No.: 001/15321 - Mechanical CAD

Trainer is the best guide for autocad and institute is also best for autocad

Vikash Kumar Sharma - Reg No.: 001/15322 - Mechanical CAD

This is professional institute , It has given the full knowledge of the subject, It is best best providing knowledge.

Dinu Kumar - Reg No.: 001/15328 - AutoCAD ME

This institute given me knowledge about Autocad and I appreciate that . I must recommend other to learn CAD from here.  

Abhishek Kr Singh (Reg No . 001/15332)

I am Gracy Pradhan , completed my  Linux training on RHCE from GenCor. Training is fully practical based and full syllabus coverage prescribed RED Hat Inc. . This training is surely going to provide me help in placement by college.

Gracy Pradhan - Ram Krishna Dharmarth Foundation University

I completed my Autocad Civil Training with Project and certification from GenCor InfoEdge – India, Trainer is The best in providing training .

Sonu Kumar

Best Training for telecommunication on Cisco CCNA. I got placed in 3i infotech Patna.