# `union`

If we want to use the same [object](object), a.k.a. data storage for different datatypes, which can save memory.


:::{literalinclude} ../code/union.c
:language: c
:::
:::{literalinclude} ../code/union.txt
:language: text
:::

That the output is different dependent on the data type is similar to the behavior we have seen in section {ref}`data-types`.

:::{note}
You can also create a `union` tag or new type similar to `struct`.
:::

:::{activity}  Retake: Different interpretations of a byte
We have demonstrated [different interpretations of a byte](different-interpretations-of-a-byte) before.

1. Recreate the same output using an union using the template below.
1. Explain the output.

```{literalinclude} ../code/masked/union-of-a-signed-and-unsigned-char.c
:language: c
```
The output should be:
```{literalinclude} ../code/union-of-a-signed-and-unsigned-char.txt
:language: text
```
:::

## Appendix
- Another use of `union`: [pseudo polymorphism](https://stackoverflow.com/a/252568/13870816)