I read that the sizeof a union is the largest of it's members. However, in the code below the union is larger than any of it's members. I only see this when using amd64 linux. I am compiling as gcc -o union.o union.c
Code:
#include <stdio.h>
typedef union _NESTEDU
{
long l;
int f;
} NESTEDU;
typedef struct _NOTHING
{
int a;
int b;
int c;
} NOTHING;
typedef union _UNION
{
int i;
char c;
NESTEDU n;
NOTHING k;
} UNION;
int main()
{
printf("struct/union sizeof testing\n");
printf("sizeof() int = %d\n", sizeof(int));
printf("sizeof() char = %d\n", sizeof(char));
printf("sizeof() UNION = %d\n", sizeof(UNION));
printf("sizeof() NESTEDU = %d\n", sizeof(NESTEDU));
printf("sizeof() NOTHING = %d\n", sizeof(NOTHING));
return(0);
}
And the output:
Code:
struct/union sizeof testing
sizeof() int = 4
sizeof() char = 1
sizeof() UNION = 16
sizeof() NESTEDU = 8
sizeof() NOTHING = 12
I am new to C so I may be missing something fundamental. Thanks for your help!