This blog is under construction

Tuesday 23 July 2013

C program to illustrate the difference between structures and unions

Write a C program to demonstrate the difference between structures and unions.


  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>

  /* structure with integer and character array data elements */
  struct empStruct {
        int age;
        char name[32];
  };

  /* union with integer and character array data elements */
  union empUnion {
        int age;
        char name[32];
  };

  int main() {
        struct empStruct emp1;
        union empUnion emp2;

        /*
         * size of structure is equal to the sum of size of all
         * elements in the structure.  Size of Union is the size
         * of largest element in the union.
         */
        printf("First Difference - Size:\n");
        printf("Structure empStruct:\n");
        printf("sizeof(struct empStruct): %d\n", sizeof(struct empStruct));
        printf("sizeof(emp1.age): %d\n", sizeof(emp1.age));
        printf("sizeof(emp1.name): %d\n", sizeof(emp1.name));
        printf("sizeof(struct empStruct) <=> "
                         "sizeof(emp1.age) + sizeof(emp1.name)\n\n");

        printf("Union empUnion:\n");
        printf("Size of Union(empUnion): %d\n", sizeof(union empUnion));
        printf("sizeof(emp2.age): %d\n", sizeof(emp2.age));
        printf("sizeof(emp2.name): %d\n", sizeof(emp2.name));
        printf("sizeof(struct empStruct) <=> "
                           "sizeof(emp2.name)[type with large size]\n");
        /*
         * only one element can be accessed at a time in union.
         * All variables can be accessed at a time in structure.
         */
        printf("\nSecond Difference - Access Permission:\n");
        printf("Assigning 21 to emp1.age\n");
        emp1.age = 21;
        printf("Assiging Ram to emp1.name\n");
        strcpy(emp1.name, "Ram");
        printf("\nContents in struct empStruct:\n");
        printf("struct empStruct {\n");
        printf("\tage = %d\n", emp1.age);
        printf("\tname = %s\n};\n\n", emp1.name);

        printf("Assigning 21 to emp2.age\n");
        emp2.age = 21;
        printf("Assigning ABCDEFG to emp2.name\n");

        /*
         * contents in emp2.name will overwrite the contents in
         * emp2.age.  Check the output for better understanding.  So,
         * emp2.age will have 0x41424344.  Its nothing but the
         * ASCII values of A, B, C and D.  Size of integer is 4 bytes
         * So, emp2.age will have first 4 characters in emp2.name
         */
        strcpy(emp2.name, "ABCDEFG");

        printf("\nContents in union empUnion:\n");
        printf("union empUnion {\n");
        printf("\tage = 0x%x\n", emp2.age);
        printf("\tname = %s\n};\n\n", emp2.name);
        printf("\nASCII value of A: 0x%x\n", 'A');
        printf("ASCII value of B: 0x%x\n", 'B');
        printf("ASCII value of C: 0x%x\n", 'C');
        printf("ASCII value of D: 0x%x\n", 'D');
        return 0;

  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  First Difference - Size:
  Structure empStruct:
  sizeof(struct empStruct): 36
  sizeof(emp1.age): 4
  sizeof(emp1.name): 32
  sizeof(struct empStruct) <=> sizeof(emp1.age) + sizeof(emp1.name)

  Union empUnion:
  Size of Union(empUnion): 32
  sizeof(emp2.age): 4
  sizeof(emp2.name): 32
  sizeof(struct empStruct) <=> sizeof(emp2.name)[type with large size]
  
  Second Difference - Access Permission:
  Assigning 21 to emp1.age
  Assiging Ram to emp1.name

  Contents in struct empStruct:
  struct empStruct {
age = 21
name = Ram
  };

  Assigning 21 to emp2.age
  Assigning ABCDEFG to emp2.name

  Contents in union empUnion:
  union empUnion {
age = 0x41424344  //ASCII value of A,B,C & D
name = ABCDEFG
  };

  ASCII value of A: 0x41
  ASCII value of B: 0x42
  ASCII value of C: 0x43
  ASCII value of D: 0x44


No comments:

Post a Comment