Sunday, May 1, 2016

Size Of Union


Implement A C Code That Take a Simple Union and print the size and the changes on each union element after entering data.



Remember : Union Size Equal To Largest Union Element Size.


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

union student {
    char name[20];
    int age;
    float gpa;
};

int main () {
    union student x;
    printf("The Size Of x is %d\n",sizeof(x));
    strcpy(x.name,"Max John");
    printf("Name=%s Age=%d GPA=%f\n",x.name,x.age,x.gpa);
    x.age=23;
    printf("Name=%s Age=%d GPA=%f\n",x.name,x.age,x.gpa);
    x.gpa=3.2;
    printf("Name=%s Age=%d GPA=%f\n",x.name,x.age,x.gpa);
    return 0 ;
}








Share this

0 Comment to "Size Of Union"

Post a Comment