struct: A variable that stores different types of data items
Shared body: A variable that stores different types of data items, the same memory location, and stores different data types
#include <stdio.h>#include<string.h>structBooks {Chartitle[ -]; Charauthor[ -]; }; Union Data {Chartitle[ -]; Charauthor[ -]; }; Main () {Union Data _u; structBooks _s; printf ("struct:%d\n",sizeof(_s)); printf ("Union size:%d\n",sizeof(_u)); /*********************struct***********************/strcpy (_s.title,"C Programming"); printf ("union:title:%s\n", _s.title); strcpy (_s.author,"Andy Bob"); printf ("union:title:%s\n", _s.title); /*********************union***********************/strcpy (_u.title,"C Programming"); printf ("struct:title:%s\n", _u.title); //be overwrittenstrcpy (_u.author,"Rectification"); printf ("struct:title:%s\n", _u.title); return 0; }
Struct:70
Union Size: 50
UNION:TITLE:C programming
UNION:TITLE:C programming
STRUCT:TITLE:C programming
Struct:title: Rectification
C Language: structure, common body