A sample using union in C

2010-09-03


This sample code is for testing some data type border value using C union. Worked on Linux OS.

#include
#include

int main(void)
{
union
{
unsigned long int lms;
unsigned short shms[2];
unsigned char  bms [4];
} myms;

myms.lms = 0;

while (1)
{
printf("long: %ld  short: %d  byte: %d\n", myms.lms, myms.shms[0], myms.bms[0]);
usleep(900);
myms.lms++;
}
return 0;
}