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:
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:
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:
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
You should read it
- The clause combines UNION data in SQL
- UNION ALL operator in SQL Server
- UNION operator in SQL Server
- What is Western Union?
- Types of money in Mobile Union
- How to Watch the State of the Union Address
- Top 10 most powerful generals in Mobile Union
- Find out about Western Union
- The European Union aims to 'climate balance' by 2050
- The mysterious Petrozavodsk event: UFOs appeared in the Soviet Union during the Cold War
- Instructions on how to play Mobile Union on computer
- The list of champions is in the Union Flag
Maybe you are interested
How to Make Doughnut Seeds (Prank) Effectively use table features in Excel 2010 Turn off the message Send message without a subject in Outlook 2010 When you are hungry, you should eat 6 fruits that will be extremely healthy Great way to cool cars 60 degrees after only 30 seconds The fruits will be wrongly selected based on color