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.

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:

Union in C Picture 1

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:

Union in C Picture 2

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:

Union in C Picture 3

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

5 ★ | 2 Vote

May be interested

  • Bit field in CPhoto of Bit field in C
    suppose your c program includes a number of true / false variables grouped in a structure called a page to check if the manufactured goods have sufficient width and height allowed.
  • Keyword typedef in CPhoto of Keyword typedef in C
    the c program language provides a typedef keyword, which you can use to provide the type for a new name. here is an example to define a byte entry for 1-byte numbers (like unsigned char).
  • Input & Output in CPhoto of Input & Output in C
    when we talk about input, we are talking about input data for the program. it can be provided from the command line or from a certain file. program c language provides a set of functions available to read the entered data and provide it for the required programs.
  • Read - Write File in CPhoto of Read - Write File in C
    the previous chapter explained the standard input and output devices handled by the c language. in this chapter we will see how programmers create, open and close text files or binary files with data. storage.
  • Pre-processor in CPhoto of Pre-processor in C
    the preprocessor in c here is not part of the compiler, but there are separate steps in the compilation process. in the most basic way, the preprocessor in c language is the text replacement tool and the compiler instruction does not require pre-processing before being compiled.
  • Header File in CPhoto of Header File in C
    a file header is a file with the .h format containing function declarations and macro definitions and can be shared across multiple source files. there are two types of file headers: the file that the programmer writes and the file that comes with your compiler.