What is the difference between structure and union in C?

StructureUnion
Each member has separate memoryAll members share the same memory
Size is the sum of members (plus padding)Size is the size of the biggest member
All members hold values togetherOnly one member holds a valid value
#include <stdio.h>

struct S { int i; double d; };
union  U { int i; double d; };

int main(void) {
    printf("%zu %zu\n", sizeof(struct S), sizeof(union U));
    return 0;
}

Output:

Output of the above program is

16 8

See Structures and Union.


Learn more about the similar topics:
Tutorials
C Structures
C Nested Structure
C Array of Structures
C Pointer to Structure
C Union
C typedef
C enum
Exercises & Assignments
Write a C program to store and print student details using a structure
Interview Questions & Answers
What is the difference between structure and union in C?
What is typedef in C?
What is an enum in C?
What is structure padding in C?