O'Reilly logo

Mastering Arduino by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Unions

A union is a special data type that enables us to store different data types in a single definition, similar to the structure; however, only one of the members may contain data at any one time. The following shows the syntax for defining a union:

union name {
  variable list
  .
  .
};

If the syntax looks a lot like the syntax for a structure. In fact, it is the same syntax except for the struct/union keywords.

Let's see how we would use a union. The following code defines a new union:

union some_data {
  int i;
  double d;
  char s[20];
};

The preceding code defines a union named some_data that can contain an integer, double or a character string. The keyword in that last sentence is the or. Unlike the structure, which can store several different ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required