/* * ===================================================================================== * * Filename: union.c * * Description: union * * Version: 1.0 * Created: 11/30/2011 05:12:43 PM * Revision: none * Compiler: gcc * * Author: Milan Kabat (), kabat@ics.muni.cz * Company: FI MUNI * * ===================================================================================== */ #include #include struct chars { unsigned char a, b, c, d; }; union test { struct chars bytes; unsigned int number; }; /* * === FUNCTION ====================================================================== * Name: main * Description: * ===================================================================================== */ int main ( int argc, char *argv[] ) { printf("\n"); union test test1; test1.bytes.a = 'A'; test1.bytes.b = 'B'; test1.bytes.c = 'C'; test1.bytes.d = 'D'; printf("Znaky: %c %c %c %c\n", test1.bytes.a, test1.bytes.b, test1.bytes.c, test1.bytes.d); printf("Bajty: %d %d %d %d\n", test1.bytes.a, test1.bytes.b, test1.bytes.c, test1.bytes.d); printf("Cislo: %u\n", test1.number); test1.number = 843330130; //Pozor, kodovacia tabulka obsahuje i znaky, ktore sa nedaju //vytlacit na vystup. Skuste menit tuto hodnotu a pozorovat //vystup ... printf("\n"); printf("Znaky: %c %c %c %c\n", test1.bytes.a, test1.bytes.b, test1.bytes.c, test1.bytes.d); printf("Bajty: %d %d %d %d\n", test1.bytes.a, test1.bytes.b, test1.bytes.c, test1.bytes.d); printf("Cislo: %u\n", test1.number); printf("\n\n"); return EXIT_SUCCESS; } /* ---------- end of function main ---------- */