Skip to content

Instantly share code, notes, and snippets.

@hackrio1
Created September 1, 2018 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hackrio1/354ba5c1aaf9a0f4f3a6ebab1e24dc6b to your computer and use it in GitHub Desktop.
Save hackrio1/354ba5c1aaf9a0f4f3a6ebab1e24dc6b to your computer and use it in GitHub Desktop.
#include <stdio.h>
union PersonUnion {
int height;
double weight;
};
struct PersonStruct {
int height;
double weight;
};
int main() {
union PersonUnion person1_union = {1700};
union PersonUnion person2_union = {.weight=74.23};
printf("Details of Union\n");
printf("Height of person1_union: %d\n", person1_union.height);
printf("Weight of person2_union: %f\n", person2_union.weight);
printf("Size of person1_union object: %lu\n", sizeof(person1_union));
printf("Size of person2_union object: %lu\n", sizeof(person2_union));
printf("\n");
struct PersonStruct person1_struct = {1700, 0.0};
struct PersonStruct person2_struct = {0, 74.23};
printf("Details of Struct\n");
printf("Height of person1_struct: %d\n", person1_struct.height);
printf("Weight of person2_struct: %f\n", person2_struct.weight);
printf("Size of person1_struct object: %lu\n", sizeof(person1_struct));
printf("Size of person2_struct object: %lu\n", sizeof(person2_struct));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment