Script started on Wed Oct 28 17:29:48 2026 Restored session: Mon Oct 26 18:45:10 EDT 2026 macbook:logs> cd ../programs/C_ch17 macbook:C_ch17> cat ~/Desktop/q17.txt A struct with four floats has a size of 16. Should a union of four floats also have a size of 16? macbook:C_ch17> cat malloc_ex1.c #include #include int main() { char *p; p = malloc(10000); if (p == NULL) { printf("Could not allocate memory\n"); exit(0); } printf("Memory allocation worked\n"); free(p); return 0; } macbook:C_ch17> gcc malloc_ex1.c macbook:C_ch17> ./a.out Memory allocation worked macbook:C_ch17> cat malloc_ex2.c #include #include int main() { //char p[] = malloc(10000); char p[10000]; p = malloc(10000); // this does not really work if (p[0] == NULL) { printf("Could not allocate memory\n"); exit(0); } printf("Memory allocation worked\n"); free(p); return 0; } macbook:C_ch17> cat C_ch17_10.c #include int main() { char *p = malloc(1024); if (p == NULL) { printf("Could not allocate memory\n"); exit(2); } fp = fopen("test.txt", "r"); // Always check to see if the command worked. if (fp == NULL) { // It did not work, so quit. printf("Error: fopen command failed\n"); exit(1); } // .. do some file stuff // Close the stream fclose(fp); free(p); return 0; } macbook:C_ch17> gcc C_ch17_10.c C_ch17_10.c:6:13: error: call to undeclared library function 'malloc' with type 'void *(unsigned long)'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] 6 | char *p = malloc(1024); | ^ C_ch17_10.c:6:13: note: include the header or explicitly provide a declaration for 'malloc' C_ch17_10.c:9:6: error: call to undeclared library function 'exit' with type 'void (int) __attribute__((noreturn))'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] 9 | exit(2); | ^ C_ch17_10.c:9:6: note: include the header or explicitly provide a declaration for 'exit' C_ch17_10.c:12:3: error: use of undeclared identifier 'fp' 12 | fp = fopen("test.txt", "r"); | ^ C_ch17_10.c:14:7: error: use of undeclared identifier 'fp' 14 | if (fp == NULL) { | ^ C_ch17_10.c:22:10: error: use of undeclared identifier 'fp' 22 | fclose(fp); | ^ C_ch17_10.c:24:3: error: call to undeclared library function 'free' with type 'void (void *)'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] 24 | free(p); | ^ C_ch17_10.c:24:3: note: include the header or explicitly provide a declaration for 'free' 6 errors generated. macbook:C_ch17> vi C_ch17_10.c macbook:C_ch17> gcc C_ch17_10.c C_ch17_10.c:13:3: error: use of undeclared identifier 'fp' 13 | fp = fopen("test.txt", "r"); | ^ C_ch17_10.c:15:7: error: use of undeclared identifier 'fp' 15 | if (fp == NULL) { | ^ C_ch17_10.c:23:10: error: use of undeclared identifier 'fp' 23 | fclose(fp); | ^ 3 errors generated. macbook:C_ch17> vi C_ch17_10.c macbook:C_ch17> gcc C_ch17_10.c macbook:C_ch17> cat C_ch17_10.c #include #include int main() { FILE *fp; char *p = malloc(1024); if (p == NULL) { printf("Could not allocate memory\n"); exit(2); } fp = fopen("test.txt", "r"); // Always check to see if the command worked. if (fp == NULL) { // It did not work, so quit. printf("Error: fopen command failed\n"); exit(1); } // .. do some file stuff // Close the stream fclose(fp); free(p); return 0; } macbook:C_ch17> ./a.out macbook:C_ch17> vi C_ch17_10.c macbook:C_ch17> gcc C_ch17_10.c macbook:C_ch17> ./a.out The first up to 255 chars are: [This is a list of courses. ] macbook:C_ch17> cat C_ch17_10.c #include #include int main() { FILE *fp; char *p = malloc(1024); if (p == NULL) { printf("Could not allocate memory\n"); exit(2); } fp = fopen("test.txt", "r"); // Always check to see if the command worked. if (fp == NULL) { // It did not work, so quit. printf("Error: fopen command failed\n"); exit(1); } // .. do some file stuff char buff[255+1]; // n=255 fgets(buff, 255, (FILE*)fp); printf("The first up to 255 chars are: [%s]\n", buff); // Close the stream fclose(fp); free(p); return 0; } macbook:C_ch17> vi C_ch17_10.c macbook:C_ch17> man fgets macbook:C_ch17> gcc C_ch17_10.c C_ch17_10.c:26:40: warning: comparison between pointer and integer ('char *' and 'int') [-Wpointer-integer-compare] 26 | while ((fgets(buff, 255, (FILE*)fp)) != EOF) { | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~ 1 warning generated. macbook:C_ch17> vi C_ch17_10.c macbook:C_ch17> gcc C_ch17_10.c macbook:C_ch17> ./a.out file: [This is a list of courses. ] file: [CSC 1010 - COMPUTERS & APPLICATIONS ] macbook:C_ch17> cat C_ch17_10.c #include #include int main() { FILE *fp; char *p = malloc(1024); if (p == NULL) { printf("Could not allocate memory\n"); exit(2); } fp = fopen("test.txt", "r"); // Always check to see if the command worked. if (fp == NULL) { // It did not work, so quit. printf("Error: fopen command failed\n"); exit(1); } // .. do some file stuff char buff[255+1]; // n=255 while ((fgets(buff, 255, (FILE*)fp)) != NULL) { //fgets(buff, 255, (FILE*)fp); printf("file: [%s]\n", buff); } // Close the stream fclose(fp); free(p); return 0; } macbook:C_ch17> cat ~/Desktop/q18.txt Suppose that we have the code: enum {ASH, MAPLE, OAK, HICKORY}; int mytree = MAPLE; What value does "mytree" have? a. -1 b. 0 c. 1 d. undefined e. this will cause a compiler error macbook:C_ch17> exit Saving session... ...saving history...truncating history files... ...completed. Deleting expired sessions... 1 completed. Script done on Wed Oct 28 18:47:00 2026