boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

082-union-basic.c (531B)


      1 /* Union: shared storage, member access. CC.md §Types lists union. */
      2 
      3 union Box { int i; char c[4]; };
      4 
      5 int test_int_then_char(void) {
      6     union Box u;
      7     u.i = 0;
      8     u.c[0] = 1;
      9     u.c[1] = 2;
     10     u.c[2] = 3;
     11     u.c[3] = 4;
     12     /* On LP64 little-endian: 0x04030201 */
     13     return u.i == 0x04030201 ? 0 : 1;
     14 }
     15 
     16 int test_size(void) {
     17     return sizeof(union Box) == 4 ? 0 : 2;
     18 }
     19 
     20 int main(int argc, char **argv) {
     21     int r = test_int_then_char();
     22     if (r) return r;
     23     r = test_size();
     24     if (r) return r;
     25     return 0;
     26 }