A C language key phrase worthy of shunning is union. It joins goto as one other key phrase to keep away from, however union is extra within the realm of the deprecated will get() perform in that it has a possible for harmful exploits. So why not have some enjoyable with it earlier than the C Lords banish it from the language?
No, I’ve no perception as as to whether the union key phrase can be banished within the subsequent C language replace. It’s, nonetheless, on the record of issues to keep away from as it may be exploited as a weak spot.
I’ve written in regards to the union key phrase earlier than. As a abstract, it’s an information container just like a construction in its declaration. Just like the construction, a union comprises identifiers. However not like a construction, the union’s identifiers share the identical information area. So whereas a construction hosts a number of members of various sorts every with its personal area, a union holds a a number of members of various sorts sharing a single area. It’s the one area half that’s harmful: You may set information of 1 kind into the union and entry it by way of one other kind. Such information typing inconsistency isn’t current in trendy programming languages neither is there any compiler oversight relating to how the information is interpreted.
The next code illustrates a union with two members, an integer and a personality:
2026_09_19-Lesson.c
#embody <stdio.h>
int primary()
{
union stow {
int l;
char c;
} s;
s.l = 0x87654321;
printf("Union 's' shops: 0xpercentXn",s.l);
printf("But additionally: %cn",s.c);
return 0;
}
The union stow comprises two members: int l and char c. Variable s is created because the union is asserted.
Hex worth 0x87654321 is assigned to member s.l. The primary printf() assertion outputs this worth. The second printf() assertion accesses member s.c, the character, and outputs its worth. Each member use the identical information saved within the union’s area to generate this output:
Union 's' shops: 0x87654321 But additionally: !
The space for storing for union stow is massive sufficient to comprise an integer worth, 32 bits. Determine 1 illustrates how this storage might look.
Each members of the union share the space for storing, however entry solely a related half.
Integer s.l shops its full worth within the area allotted for the union, 4 bytes. Character s.c makes use of solely a kind of bytes. As a result of the values share the identical space for storing, 0x21 is assigned to s.c when 0x87654321 is ready for member s.l.
To substantiate the union’s measurement, I added this line to the code:
printf("Union 's' is %lu bytesn",sizeof(s));
Right here is the up to date output:
Union 's' shops: 0x87654321 But additionally: ! Union 's' is 4 bytes
The sizeof operator confirms that union s occupies 4 bytes of storage, the dimensions of an int, its largest member.
Subsequent week I proceed my exploration of the union, with an instance of unions had been as soon as an vital a part of C programming again within the early PC days.

