Script started on Mon Oct 26 17:29:22 2026 Restored session: Wed Oct 21 18:42:01 EDT 2026 macbook:Desktop> cat q16.c #include #include #define NAME_LEN 100 typedef struct { int number; char name[NAME_LEN + 1]; } Part; int main() { Part part1, part2; strncpy(part1.name, "hammer", 7); part2 = part1; // Question: what will this print? printf("%s\n", part2.name); return 0; } macbook:Desktop> gcc q16.c macbook:Desktop> ./a.out hammer macbook:Desktop> cat float_in_hex.c cat: float_in_hex.c: No such file or directory macbook:Desktop> cd csc3320_Fall2026/programs/C_ch16 macbook:C_ch16> cat float_in_hex.c // How does a float look internally? // -MCW #include union { float value; unsigned char bytes[4]; } my_union; int main() { my_union.value = 10.7; printf("float value is %f \n", my_union.value); printf("float value in hex is "); for (int i=0; i<4; i++) printf("%2x ", my_union.bytes[i]); printf("\n"); my_union.value = 10.8; printf("float value is %f \n", my_union.value); printf("float value in hex is "); for (int i=0; i<4; i++) printf("%2x ", my_union.bytes[i]); printf("\n"); return 0; } macbook:C_ch16> gcc float_in_hex.c macbook:C_ch16> ./a.out float value is 10.700000 float value in hex is 33 33 2b 41 float value is 10.800000 float value in hex is cd cc 2c 41 macbook:C_ch16> cat sizeof_union_struct.c // Show the sizes of the union vs struct #include int main() { union { int i; float f; char c; double val; long L_val; } u; struct { int i; float f; char c; double val; long L_val; } s; // Do not need to assign values, since we just want to see size printf("Size of struct s is %lu\n", sizeof(s)); printf("Size of union u is %lu\n", sizeof(u)); return 0; } macbook:C_ch16> gcc sizeof_union_struct.c macbook:C_ch16> ./a.out Size of struct s is 32 Size of union u is 8 macbook:C_ch16> cat C_ch16_8_v2.c #include int main() { union { int i; float f; } u; struct { int i; float f; } s; s.i = 3; s.f = 5.6; printf("s.i is %d \n", s.i); printf("s.f is %f \n", s.f); u.i = 3; u.f = 5.6; printf("u.i is %d \n", u.i); printf("u.i is %x in hex\n", u.i); printf("u.f is %f \n", u.f); return 0; } macbook:C_ch16> gcc C_ch16_8_v2.c macbook:C_ch16> ./a.out s.i is 3 s.f is 5.600000 u.i is 1085485875 u.i is 40b33333 in hex u.f is 5.600000 macbook:C_ch16> cp float_in_hex.c float_in_hex_v2.c macbook:C_ch16> vi float_in_hex_v2.c macbook:C_ch16> gcc float_in_hex_v2.c macbook:C_ch16> ./a.out float value is 5.600000 float value in hex is 33 33 b3 40 macbook:C_ch16> cat float_in_hex_v2.c // How does a float look internally? // -MCW #include union { float value; unsigned char bytes[4]; } my_union; int main() { // see C_ch16_8_v2.c my_union.value = 5.6; printf("float value is %f \n", my_union.value); printf("float value in hex is "); for (int i=0; i<4; i++) printf("%2x ", my_union.bytes[i]); printf("\n"); return 0; } macbook:C_ch16> cat C_ch16_11.c /* See Kim King's book, page 403. This is an expanded example with code added by Michael Weeks. */ #include enum {CLUBS, DIAMONDS, HEARTS, SPADES} s; void print_suit(int s1) { switch (s1) { case CLUBS: printf("Clubs"); break; case DIAMONDS: printf("Diamonds"); break; case HEARTS: printf("Hearts"); break; case SPADES: printf("Spades"); break; default: printf("Unknown"); } } int main() { int i; i = DIAMONDS; /* i is now 1 */ printf("i = %d, which is ", i); print_suit(i); printf("\n"); s = 0; /* s is now 0 (CLUBS) */ printf("s = %d, which is ", s); print_suit(s); printf("\n"); s++; /* s is now 1 (DIAMONDS) */ printf("s = %d, which is ", s); print_suit(s); printf("\n"); i = s + 2; /* i is now 3 */ printf("i = %d, which is ", i); print_suit(i); printf("\n"); return 0; } macbook:C_ch16> gcc C_ch16_11.c macbook:C_ch16> ./a.out i = 1, which is Diamonds s = 0, which is Clubs s = 1, which is Diamonds i = 3, which is Spades macbook:C_ch16> macbook:C_ch16> cat C_ch16_12.c struct point { int x; int y; }; struct rectangle { struct point upper_left, lower_right; }; macbook:C_ch16> cp C_ch16_12.c find_area.c macbook:C_ch16> echo "#include " >> find_area.c macbook:C_ch16> echo "int main() {" >> find_area.c macbook:C_ch16> echo "return 0;" >> find_area.c macbook:C_ch16> vi find_area.c macbook:C_ch16> gcc find_area.c find_area.c:14:20: error: parameter 'my_rect' was not declared, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int] 14 | float compute_area(my_rect) { | ^ find_area.c:17:18: error: member reference base type 'int' is not a structure or union 17 | pt1 = my_rect.upper_left; | ~~~~~~~^~~~~~~~~~~ find_area.c:18:18: error: member reference base type 'int' is not a structure or union 18 | pt2 = my_rect.lower_right; | ~~~~~~~^~~~~~~~~~~~ find_area.c:14:7: warning: a function definition without a prototype is deprecated in all versions of C and is not supported in C23 [-Wdeprecated-non-prototype] 14 | float compute_area(my_rect) { | ^ find_area.c:46:28: error: passing 'struct rectangle' to parameter of incompatible type 'int' 46 | my_area = compute_area(my_rect); | ^~~~~~~ 1 warning and 4 errors generated. macbook:C_ch16> vi find_area.c macbook:C_ch16> gcc find_area.c macbook:C_ch16> ./a.out the area is 9.000000 macbook:C_ch16> cat find_area.c #include struct point { int x; int y; }; struct rectangle { struct point upper_left, lower_right; }; float compute_area(struct rectangle my_rect) { struct point pt1; struct point pt2; pt1 = my_rect.upper_left; pt2 = my_rect.lower_right; return (pt2.x - pt1.x) * (pt1.y - pt2.y); } /* | | * (7,6) | | * (10,3) | ------------------- */ int main() { struct rectangle my_rect; struct point pt; pt.x = 7; pt.y = 6; my_rect.upper_left = pt; pt.x = 10; pt.y = 3; my_rect.lower_right = pt; float my_area; my_area = compute_area(my_rect); printf(" the area is %f\n", my_area); return 0; } macbook:C_ch16> vi find_area.c macbook:C_ch16> cat find_area.c #include struct point { int x; int y; }; struct rectangle { struct point upper_left, lower_right; }; float compute_area(struct rectangle my_rect) { struct point pt1; struct point pt2; pt1 = my_rect.upper_left; pt2 = my_rect.lower_right; return (pt2.x - pt1.x) * (pt1.y - pt2.y); } /* | | * (7,6) | | * (11,3) | ------------------- */ int main() { struct rectangle my_rect; struct point pt; pt.x = 7; pt.y = 6; my_rect.upper_left = pt; pt.x = 11; pt.y = 3; my_rect.lower_right = pt; float my_area; my_area = compute_area(my_rect); printf(" the area is %f\n", my_area); return 0; } macbook:C_ch16> gcc find_area.c macbook:C_ch16> ./a.out the area is 12.000000 macbook:C_ch16> exit Saving session... ...saving history...truncating history files... ...completed. Deleting expired sessions... 13 completed. Script done on Mon Oct 26 18:45:10 2026