Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Union in C

Explore Union in C with clear explanations, practical examples, and useful tips.

Table of Contents

This guide covers union in c with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.

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

FAQ

What should I check before following these steps?

Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.

Why might the process not work?

Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.

Can I undo the changes if necessary?

That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.