|
| 1 | +#include <stdio.h> |
| 2 | +#include <string.h> |
| 3 | + |
| 4 | +// declaring structure |
| 5 | +struct struct_example |
| 6 | +{ |
| 7 | + int integer; |
| 8 | + float decimal; |
| 9 | + char name[20]; |
| 10 | +}; |
| 11 | + |
| 12 | +// declaring union |
| 13 | + |
| 14 | +union union_example |
| 15 | +{ |
| 16 | + int integer; |
| 17 | + float decimal; |
| 18 | + char name[20]; |
| 19 | +}; |
| 20 | + |
| 21 | +void main() |
| 22 | +{ |
| 23 | + // creating variable for structure |
| 24 | + // and initializing values difference |
| 25 | + // six |
| 26 | + struct struct_example s={18,38,"nazusecurity"}; |
| 27 | + |
| 28 | + // creating variable for union |
| 29 | + // and initializing values |
| 30 | + union union_example u={18,38,"nazusecurity"}; |
| 31 | + |
| 32 | + |
| 33 | + printf("structure data:\n integer: %d\n" |
| 34 | + "decimal: %.2f\n name: %s\n", |
| 35 | + s.integer, s.decimal, s.name); |
| 36 | + printf("\nunion data:\n integer: %d\n" |
| 37 | + "decimal: %.2f\n name: %s\n", |
| 38 | + u.integer, u.decimal, u.name); |
| 39 | + |
| 40 | + |
| 41 | + // difference two and three |
| 42 | + printf("\nsizeof structure : %d\n", sizeof(s)); |
| 43 | + printf("sizeof union : %d\n", sizeof(u)); |
| 44 | + |
| 45 | + // difference five |
| 46 | + printf("\n Accessing all members at a time:"); |
| 47 | + s.integer = 183; |
| 48 | + s.decimal = 90; |
| 49 | + strcpy(s.name, "nazusecurity"); |
| 50 | + |
| 51 | + printf("structure data:\n integer: %d\n " |
| 52 | + "decimal: %.2f\n name: %s\n", |
| 53 | + s.integer, s.decimal, s.name); |
| 54 | + |
| 55 | + u.integer = 183; |
| 56 | + u.decimal = 90; |
| 57 | + strcpy(u.name, "nazusecurity"); |
| 58 | + |
| 59 | + printf("\nunion data:\n integer: %d\n " |
| 60 | + "decimal: %.2f\n name: %s\n", |
| 61 | + u.integer, u.decimal, u.name); |
| 62 | + |
| 63 | + printf("\n Accessing one member at time:"); |
| 64 | + |
| 65 | + printf("\nstructure data:"); |
| 66 | + s.integer = 240; |
| 67 | + printf("\ninteger: %d", s.integer); |
| 68 | + |
| 69 | + s.decimal = 120; |
| 70 | + printf("\ndecimal: %f", s.decimal); |
| 71 | + |
| 72 | + strcpy(s.name, "C programming"); |
| 73 | + printf("\nname: %s\n", s.name); |
| 74 | + |
| 75 | + printf("\n union data:"); |
| 76 | + u.integer = 240; |
| 77 | + printf("\ninteger: %d", u.integer); |
| 78 | + |
| 79 | + u.decimal = 120; |
| 80 | + printf("\ndecimal: %f", u.decimal); |
| 81 | + |
| 82 | + strcpy(u.name, "C programming"); |
| 83 | + printf("\nname: %s\n", u.name); |
| 84 | + |
| 85 | + //difference four |
| 86 | + printf("\nAltering a member value:\n"); |
| 87 | + s.integer = 1218; |
| 88 | + printf("structure data:\n integer: %d\n " |
| 89 | + " decimal: %.2f\n name: %s\n", |
| 90 | + s.integer, s.decimal, s.name); |
| 91 | + |
| 92 | + u.integer = 1218; |
| 93 | + printf("union data:\n integer: %d\n" |
| 94 | + " decimal: %.2f\n name: %s\n", |
| 95 | + u.integer, u.decimal, u.name); |
| 96 | +} |
0 commit comments