Union in C

A Union is a special data in C language that allows you to reserve different data types in the same memory. You can define Union with a lot of parameters, but only one component contains values ​​at a time. Union provides an effective way to use a device for multiple purposes.

A Union is a special data in C language that allows you to reserve different data types in the same memory. You can define Union with a lot of parameters, but only one component contains values ​​at a time. Union provides an effective way to use a device for multiple purposes.

Define a Union in C

To define a Union , you must provide union statements in the same way as you defined the structure. The union statement defines a new data type, with more than one member in your program. The form of the union command is as follows:

 union [ ten_union ] { phan dinh nghia thanh vien union ; phan dinh nghia thanh vien union ; . phan dinh nghia thanh vien union ; } [ mot hoac nhieu bien union ]; 

ten_union is an optional value and a definition is a common variable definition, such as int i, or float j and other variable definition types. At the end of the Union definition before the last semicolon, you can define one or more Union variables but it is optional. This is how you define Union type QTM with 3 members i, f and chuoi:

 union QTM { int i ; float f ; char chuoi [ 50 ]; } tenbien ; 

Now the QTM type can contain an integer, a real number and a character string. This means that it is a separate variable with the same memory area that can be used to store many different data types. You can use the available data type or define yourself within Union based on your requirements.

The memory occupied by Union is large enough to hold the value of Union's largest component. For example, in the example above the QTM type contains 20 bytes of memory because it contains the maximum memory space of the string object. Here is an example to display Union total memory on:

 #include #include union QTM { int i ; float f ; char chuoi [ 50 ]; }; int main ( ) { union QTM tenbien ; printf ( "Kich co bo nho bi chiem giu boi tenbien la: %dn" , sizeof ( tenbien )); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; } 

Compiling and running the above C program will result:

Picture 1 of Union in C

Retrieve Union member in C

To retrieve Union members you use the member access operator (.) . Member access operator is located between the variable name Union and the member you want to access. We will use the union keyword to define the type of Union variable. Here is an example for using Union:

 #include #include union QTM { int i ; float f ; char chuoi [ 50 ]; }; int main ( ) { union QTM tenbien ; tenbien . i = 15 ; tenbien . f = 25.67 ; strcpy ( tenbien . chuoi , "Hoc Lap trinh C tai QTM" ); printf ( "tenbien.i : %dn" , tenbien . i ); printf ( "tenbien.f : %fn" , tenbien . f ); printf ( "tenbien.chuoi : %sn" , tenbien . chuoi ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; } 

Compiling and running the above C program will result:

Picture 2 of Union in C

Here you can understand that Union and i member values ​​can conflict because the last value assigned to the variable is already occupied in a device and that's why you can use the value to The chuoi pellet produced good results. Now let's look at the following example when you focus on one variable at a time as the main purpose for using Union.

 #include #include union QTM { int i ; float f ; char chuoi [ 50 ]; }; int main ( ) { union QTM tenbien ; tenbien . i = 15 ; printf ( "tenbien.i : %dn" , tenbien . i ); tenbien . f = 25.67 ; printf ( "tenbien.f : %fn" , tenbien . f ); strcpy ( tenbien . chuoi , "Hoc Lap trinh C tai QTM" ); printf ( "tenbien.chuoi : %sn" , tenbien . chuoi ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; } 

Compiling and running the above C program will result:

Picture 3 of Union in C

Here, all members are printed very well because one member is used at a time.

According to Tutorialspoint

Previous article: What is C programming language?

Next lesson: Bit field in C

Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile