Script started on Mon Oct 28 12:32:10 2024 mweeks@air:C_ch16$ echo 2734 2734 mweeks@air:C_ch16$ echo "floating point value +/- exponent significand" floating point value +/- exponent significand mweeks@air:C_ch16$ echo "+1.0001 x 2^3 stored + 3 (biased) 1.0001" +1.0001 x 2^3 stored + 3 (biased) 1.0001 mweeks@air:C_ch16$ echo "sign exp significand -> b16 " sign exp significand -> b16 mweeks@air:C_ch16$ vi notes.txt mweeks@air:C_ch16$ mv notes.txt union_notes.txt mweeks@air:C_ch16$ cat union_notes.txt >> disp(sprintf('%tx', 10.7)) 412b3333 the value 10.7 is stored as 41 2b 33 33 internally union { float value; char bytes[4]; } my_union; // ... my_union.value = 10.7; my_union.bytes[2] = 0; my_union.bytes[3] = 0; mweeks@air:C_ch16$ cat C_ch16_1.c struct { int number; char name[NAME_LEN + 1]; int on_hand; } part1, part2; printf("Part number : %d \ n", part1.number); part1.number = 258; part1.on_hand++; scanf("%d",&part1.on_hand); // &(part1.on_hand) part1 = part2 // The members in part 1 and part 2 // then have the same values mweeks@air:C_ch16$ cat C_ch16_2.c struct part { int number; char name[NAME_LEN + 1]; int on_hand; } struct part part1, part2; part part1, part2; // WRONG mweeks@air:C_ch16$ cat C_ch16_3.c typedef struct { int number; char name[NAME_LEN + 1]; int on_hand; } Part; Part part1, part2; mweeks@air:C_ch16$ cat C_ch16_4.c void print_part(struct part p) { printf("Part number: %d\n", p.number); } mweeks@air:C_ch16$ cat C_ch16_5.c void update_part(struct part *p) { p -> number = 123; } mweeks@air:C_ch16$ vi C_ch16_4b.c mweeks@air:C_ch16$ gcc C_ch16_4b.c C_ch16_4b.c:12:24: warning: declaration of 'struct part' will not be visible outside of this function [-Wvisibility] void print_part(struct part p) ^ C_ch16_4b.c:12:29: error: variable has incomplete type 'struct part' void print_part(struct part p) ^ C_ch16_4b.c:12:24: note: forward declaration of 'struct part' void print_part(struct part p) ^ C_ch16_4b.c:17:25: warning: declaration of 'struct part' will not be visible outside of this function [-Wvisibility] void update_part(struct part *p) ^ C_ch16_4b.c:19:7: error: incomplete definition of type 'struct part' p -> number = 123; ~ ^ C_ch16_4b.c:17:25: note: forward declaration of 'struct part' void update_part(struct part *p) ^ C_ch16_4b.c:24:7: error: expected ';' after expression Part part1, part2; ^ ; C_ch16_4b.c:24:8: error: use of undeclared identifier 'part1' Part part1, part2; ^ C_ch16_4b.c:24:15: error: use of undeclared identifier 'part2'; did you mean 'Part'? Part part1, part2; ^~~~~ Part C_ch16_4b.c:9:3: note: 'Part' declared here } Part; ^ C_ch16_4b.c:26:3: error: use of undeclared identifier 'part1' part1.number = 3; ^ C_ch16_4b.c:27:3: error: use of undeclared identifier 'part1' part1.on_hand = 12; ^ C_ch16_4b.c:28:14: error: use of undeclared identifier 'part1' print_part(part1); ^ C_ch16_4b.c:30:16: error: use of undeclared identifier 'part1'; did you mean 'Part'? update_part(&part1); ^~~~~ Part C_ch16_4b.c:9:3: note: 'Part' declared here } Part; ^ C_ch16_4b.c:31:14: error: use of undeclared identifier 'part1' print_part(part1); ^ C_ch16_4b.c:24:3: warning: expression result unused [-Wunused-value] Part part1, part2; ^~~~ 3 warnings and 10 errors generated. mweeks@air:C_ch16$ vi C_ch16_4b.c mweeks@air:C_ch16$ gcc C_ch16_4b.c C_ch16_4b.c:12:24: warning: declaration of 'struct Part' will not be visible outside of this function [-Wvisibility] void print_part(struct Part p) ^ C_ch16_4b.c:12:29: error: variable has incomplete type 'struct Part' void print_part(struct Part p) ^ C_ch16_4b.c:12:24: note: forward declaration of 'struct Part' void print_part(struct Part p) ^ C_ch16_4b.c:17:25: warning: declaration of 'struct Part' will not be visible outside of this function [-Wvisibility] void update_part(struct Part *p) ^ C_ch16_4b.c:19:7: error: incomplete definition of type 'struct Part' p -> number = 123; ~ ^ C_ch16_4b.c:17:25: note: forward declaration of 'struct Part' void update_part(struct Part *p) ^ C_ch16_4b.c:24:15: error: variable has incomplete type 'struct Part' struct Part part1, part2; ^ C_ch16_4b.c:24:10: note: forward declaration of 'struct Part' struct Part part1, part2; ^ C_ch16_4b.c:24:22: error: variable has incomplete type 'struct Part' struct Part part1, part2; ^ C_ch16_4b.c:24:10: note: forward declaration of 'struct Part' struct Part part1, part2; ^ 2 warnings and 4 errors generated. mweeks@air:C_ch16$ mweeks@air:C_ch16$ mweeks@air:C_ch16$ mweeks@air:C_ch16$ mweeks@air:C_ch16$ mweeks@air:C_ch16$ mweeks@air:C_ch16$ mweeks@air:C_ch16$ mweeks@air:C_ch16$ gcc C_ch16_4b.c C_ch16_4b.c:12:24: warning: declaration of 'struct Part' will not be visible outside of this function [-Wvisibility] void print_part(struct Part p) ^ C_ch16_4b.c:12:29: error: variable has incomplete type 'struct Part' void print_part(struct Part p) ^ C_ch16_4b.c:12:24: note: forward declaration of 'struct Part' void print_part(struct Part p) ^ C_ch16_4b.c:17:25: warning: declaration of 'struct Part' will not be visible outside of this function [-Wvisibility] void update_part^[[2;2R(struct Part *p) ^ C_ch16_4b.c:19:7: error: incomplete definition of type 'struct Part' p -> number = 123; ~ ^ C_ch16_4b.c:17:25: note: forward declaration of 'struct Part' void update_part(struct Part *p) ^ C_ch16_4b.c:24:15: error: variable has incomplete type 'struct Part' struct Part part1, part2; ^ C_ch16_4b.c:24:10: note: forward declaration of 'struct Part' struct Part part1, part2; ^ C_ch16_4b.c:24:22: error: variable has incomplete type 'struct Part' struct Part part1, part2; ^ C_ch16_4b.c:24:10: note: forward declaration of 'struct Part' struct Part part1, part2; ^ 2 warnings and 4 errors generated. mweeks@air:C_ch16$ vi C_ch16_4b.c mweeks@air:C_ch16$ cat C_ch16_3.c typedef struct { int number; char name[NAME_LEN + 1]; int on_hand; } Part; Part part1, part2; mweeks@air:C_ch16$ vi C_ch16_4b.c mweeks@air:C_ch16$ gcc C_ch16_4b.c mweeks@air:C_ch16$ cat C_ch16_4b.c #include #define NAME_LEN 100 typedef struct { int number; char name[NAME_LEN + 1]; int on_hand; } Part; void print_part(Part p) { printf("Part number: %d\n", p.number); } void update_part(Part *p) { p -> number = 123; } int main() { Part part1, part2; part1.number = 3; part1.on_hand = 12; print_part(part1); update_part(&part1); print_part(part1); return 0; } mweeks@air:C_ch16$ ./a.out Part number: 3 Part number: 123 mweeks@air:C_ch16$ cp C_ch16_4b.c C_ch16_4c.c mweeks@air:C_ch16$ vi C_ch16_4c.c mweeks@air:C_ch16$ cat C_ch16_4b.c #include #define NAME_LEN 100 typedef struct { int number; char name[NAME_LEN + 1]; int on_hand; } Part; void print_part(Part p) { printf("Part number: %d\n", p.number); } void update_part(Part *p) { p -> number = 123; } int main() { Part part1, part2; part1.number = 3; part1.on_hand = 12; print_part(part1); update_part(&part1); print_part(part1); return 0; } mweeks@air:C_ch16$ gcc C_ch16_4c.c C_ch16_4c.c:19:7: error: member reference type 'Part *' is a pointer; did you mean to use '->'? *p.number = 123; ~^ -> C_ch16_4c.c:19:5: error: indirection requires pointer operand ('int' invalid) *p.number = 123; ^~~~~~~~~ 2 errors generated. mweeks@air:C_ch16$ vi C_ch16_4c.c mweeks@air:C_ch16$ gcc C_ch16_4c.c C_ch16_4c.c:19:9: error: member reference type 'Part *' is a pointer; did you mean to use '->'? *(p).number = 123; ~~~^ -> C_ch16_4c.c:19:5: error: indirection requires pointer operand ('int' invalid) *(p).number = 123; ^~~~~~~~~~~ 2 errors generated. mweeks@air:C_ch16$ vi C_ch16_4c.c mweeks@air:C_ch16$ gcc C_ch16_4c.c mweeks@air:C_ch16$ ./a.out Part number: 3 Part number: 123 mweeks@air:C_ch16$ cat C_ch16_4c.c #include #define NAME_LEN 100 typedef struct { int number; char name[NAME_LEN + 1]; int on_hand; } Part; void print_part(Part p) { printf("Part number: %d\n", p.number); } void update_part(Part *p) { (*p).number = 123; } int main() { Part part1, part2; part1.number = 3; part1.on_hand = 12; print_part(part1); update_part(&part1); print_part(part1); return 0; } mweeks@air:C_ch16$ cat C_ch16_5.c void update_part(struct part *p) { p -> number = 123; } mweeks@air:C_ch16$ cat C_ch16_6.c struct part build_part(int number, const char * name, int on_hand) { struct part p; p.number = number; strcpy(p.name, name); p.on_hand = on_hand; return p; } mweeks@air:C_ch16$ cat C_ch16_7.c struct part inventory[100]; // array of part print_part(inventory[i]); // print the ith part inventory[i].number = 883; // assign 883 to the number member of inventory[i] strcpy(inventory[i].name, "Disk Drive"); // assign "Disk Drive" to the name member of // inventory[i] mweeks@air:C_ch16$ cat C_ch16_8.c union { int i; float f; } u; struct { int i; float f; } s; mweeks@air: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; } mweeks@air:C_ch16$ gcc C_ch16_8_v2.c mweeks@air: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 mweeks@air:C_ch16$ cp C_ch16_8_v2.c C_ch16_8_v3.c mweeks@air:C_ch16$ vi C_ch16_8_v3.c mweeks@air:C_ch16$ gcc C_ch16_8_v3.c mweeks@air:C_ch16$ ./a.ou bash: ./a.ou: No such file or directory mweeks@air:C_ch16$ ./a.out s.i is 23 s.f is 4.756000 u.i is 23 u.i is 17 in hex u.f is 4.756000 Final values: s.i is 23 s.f is 4.756000 u.i is 1083715879 u.i is 40983127 in hex u.f is 4.756000 mweeks@air:C_ch16$ cat C_ch16_8_v3.c #include int main() { union { int i; float f; } u; struct { int i; float f; } s; s.i = 23; printf("s.i is %d \n", s.i); s.f = 4.756; printf("s.f is %f \n", s.f); u.i = 23; printf("u.i is %d \n", u.i); printf("u.i is %x in hex\n", u.i); u.f = 4.756; printf("u.f is %f \n", u.f); printf("Final values:\n"); printf("s.i is %d \n", s.i); printf("s.f is %f \n", s.f); 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; } mweeks@air:C_ch16$ vi notes_binary.txt mweeks@air:C_ch16$ vi notes_binary.txt mweeks@air:C_ch16$ vi notes_binary.txt mweeks@air:C_ch16$ vi notes_binary.txt mweeks@air:C_ch16$ cat notes_binary.txt we had a value of 23 (decimal) we printed it as hexadecimal, and it appeared as 17 How do we know that this is correct? convert 17 (hex) to 0001 0111 (binary) multiply each bit, working from right to left, by an increasing power of 2 1 * 2^0 = 1 1 * 2^1 = 2 1 * 2^2 = 4 0 * 2^3 = 0 1 * 2^4 = 16 0 * 2^5 = 0 0 * 2^6 = 0 0 * 2^7 = 0 -------------- add 16+4+2+1 = 23 ------------------------------------------------- u.i points to addr 123 u.f also points to addr 123 s.i points to addr 124 s.f points to addr 125 memory addr value ---- ----- 123 4.756 u.i, u.f 124 23 s.i 125 4.756 s.f mweeks@air:C_ch16$ cat C_ch16_9.c struct catalog_item { int stock_number; float price; int item_type; char title[TITLE_LEN+1]; char author[AUTHOR_LEN+1]; int num_pages; char design[DESIGN_LEN+1]; int colors; int sizes; } mweeks@air:C_ch16$ cat C_ch16_10.c struct catalog_item { int stock_number; float price; int item_type; union { struct { char title[TITLE_LEN+1]; char author[AUTHOR_LEN+1]; int num_pages; } book; struct { char design[DESIGN_LEN+1]; } mug; struct { char design[DESIGN_LEN+1]; int colors; int sizes; } shirt; } item; }; mweeks@air:C_ch16$ exit exit Script done on Mon Oct 28 13:44:57 2024