Skip to content
Justice Tech School

Unions

A union shares memory between members. Only one is valid at a time.

Example
#include <stdio.h>

union V { int i; float f; };
int main(void) {
  union V v;
  v.i = 7;
  printf("%d %zu\n", v.i, sizeof v);
  return 0;
}

Result

7 4

Exercise

Shared memory type.

 V {}

Quiz Test

How big is a union?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.