> For the complete documentation index, see [llms.txt](https://hkust-robotics-team.gitbook.io/hkust-robotics-team-software-tutorial/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hkust-robotics-team.gitbook.io/hkust-robotics-team-software-tutorial/tutorial/tutorial-1-c-and-cubeide-setup/advanced-c/union.md).

# Union

I am HKUST student and a Robotics team member at the same time.

## Union

**union**: Data-type which stores data items with different types in the same memory location.

```c
union Data {
   int i;
   float f;
   char s[15];
} data;
/**
 *  `Data` is an optional union tag.
 *  `data` is a variable of type `union Data`.
 *  Declaring variables after the union is optional.
 * /
```

#### Sample Code

```c
#include <stdio.h>
#include <string.h>
 
union Data {
   int i;
   float f;
   char s[15];
};
 
int main() {
   union Data data; /* variable `data` of type Data */ 

   data.i = 1;
   data.f = 3.14;
   strcpy(data.s, "Robotics team");

   printf("data.i : %d\n", data.i);
   printf("data.f : %f\n", data.f);
   printf("data.str : %s\n", data.s);

   return 0;
}
```

Output:

```
data.i : 1868722002
data.f : 70078190219652804681100427264.000000
data.str : Robotics team
```

Note that prior to printing the data members, the final value set is `"Robotics team"` to the `char s[15]` member.

The result would be chaotic because all data members are stored in the same memory. The printed values will correspond to "Robotics team" in their own data type.
