codegurukool.com · Free coding tutorials & online compiler

Home › C Tutorial › C unions

C unions

Fields share one memory slot.

A union holds one member at a time; its size is the largest member. Writing one overwrites the others.

Example code

#include <stdio.h>
union U { int i; float f; };
int main() {
  union U u; u.i = 7;
  printf("%d %zu\n", u.i, sizeof(u));
  return 0;
}

Run this code in the online editor

← Structs and pointersC typedef →