Function Prototypes
Flags
Short answer
Short code
pointer arithmetic
100

What does fread() return?

The number of objects (of size sz) it was able to read

100

what does the -"Wall" flag do?

enables warnings

100

What allocates memory for a variable-- a declaration or a definition?

Definition

100

Given the following FILE pointer variable definition, write the code  that will open a file named "hello.txt" for read-only access and print  a message of your choice if there was an error in doing so.      

 FILE *my_file = 0;

#include <stdio.h>


int main() {

    FILE *my_file = fopen("hello.txt", "r"); // Attempt to open the file for reading

    

    if (my_file == NULL) {

        // If fopen returns NULL, there was an error opening the file

        printf("Error opening the file 'hello.txt'\n");

    } else {

        // If successful, you can proceed with reading from the file

        // Don't forget to close the file when you're done

        fclose(my_file);

    }

    

    return 0;

}


100

What is printed?

int x = 0;

int y = 0;

int *p = NULL;

p = &x;

*p = 5;

p = &y;

*p = 7;

printf("%d %d\n", x, y);


5 7

200

What is the function prototype of fread()?

fread(void *ptr, int sz, int number, File *file_ptr);

200

What does the "-g" flag do?

generate debugging information (i.e used for gdb)

200

Declare a structure, resistor_struct, which contains (in this order) an array of ID_LEN (=5) characters named id, a float named max_power, and an integer named resistance.


#define ID_LEN 5


struct resistor_struct {

    char id[ID_LEN];

    float max_power;

    int resistance;

};


200

Write code that will, without opening any file, check if a file named   "hello.txt" can be opened for read access.  Put the code inside the   'if' predicate:       

if (              ) {         

 /* Yes, we can open the file... */      

}

if (access("hello.txt", R_OK) == 0) {    

/* Yes, we can open the file for read access */ 

}

200

What is printed?

int x = 0;

int y = 0;

int *p = NULL; int *q = NULL;

p = &x;

q = p;

*q = 7;

printf("%d %d\n", x, y);

7 0

300

What is the function prototype for fseek() and the three whence constants?

fseek(FILE *file_pointer, int offset, int whence);

whence: SEEK_SET, SEEK_END, SEEK_CUR

300

Write a single, valid command with which you would use gcc to compile a C file named abc.c into an object file xyz.o with warnings treated as errors and including support for debugging.

gcc -Wall -Werror -g -c abc.c -o xyz.o

300

int array[] = { 12, 5, 3, 6, 9, 2, 4, 2}; 

int *ptr = 0; 

ptr = &(array[2]); 

printf("size = %d\n", sizeof(array));

Assuming that sizeof(int) = 4, what was displayed?


 

size = 32

300

Write code that will, without opening any file, check if a file named   "hello.txt" can be opened for write access.  Put the code inside the   'if' predicate:       

if (              ) {         

 /* Yes, we can open the file... */      

}

if (access("hello.txt", W_OK) == 0) {    

/* Yes, we can open the file for write access */ 

}

300

What gets printed?

int x = 0;

int y = 0;

int *p = NULL; int *q = NULL;

p = &y;

q = &x;

p = 2;

q = 3

printf("%d %d\n", x, y);

0 0

400
What is the fcn prototype for char *strncpy(), and what does it return?

char *strncpy(char *destination, const char *source, int n);

it returns a pointer to the string copied.

400

Write a single, valid command with which you would use gcc to compile the C file named boiler.c, link it against the object file up.o, and produce the executable named purdue with optimizations enabled and adhering to the C17 standard. 

gcc -O -std=17 -o purdue boiler.c up.o

400

Can you "run" an object file if it contains the "main()" function?

No

400

Write a function named print_reverse that will open a text file   named "hello.txt" and print each character in the file in reverse.   i.e. print the first character last and the last character first.   The function should return the number of characters in the file.   Upon any error, return -1.    HINT: Use fseek() a lot to do this.

#include <stdio.h>


int print_reverse(const char *filename) {

    FILE *file = fopen(filename, "r");

    if (file == NULL) {

        return -1; // Error opening the file

    }


    // Seek to the end of the file to determine its size

    if (fseek(file, 0, SEEK_END) != 0) {

        fclose(file);

        return -1; // Error seeking to the end of the file

    }


    // Get the size of the file

    long filesize = ftell(file);

    if (filesize == -1) {

        fclose(file);

        return -1; // Error getting the size of the file

    }


    // Read and print characters in reverse

    for (long i = filesize - 1; i >= 0; i--) {

        if (fseek(file, i, SEEK_SET) != 0) {

            fclose(file);

            return -1; // Error seeking to the specific position

        }

        int ch = fgetc(file);

        if (ch == EOF) {

            fclose(file);

            return -1; // Error reading a character

        }

        putchar(ch);

    }


    fclose(file);

    return filesize; // Return the total number of characters

}


int main() {

    int count = print_reverse("hello.txt");

    if (count >= 0) {

        printf("\nTotal characters: %d\n", count);

    } else {

        printf("An error occurred.\n");

    }

    return 0;

}


400

Write a function called 'swap' that will accept two pointers to integers   and will exchange the contents of those integer locations. 

What would happen if you called swap like this:         

int x = 5;        

swap(&x, &x);

void swap(int *a, int *b) {

int temp = *a;    

*a = *b;   

*b = temp; 

}


x will remain 5.

500

What do many functions found in the string library (with prototypes in string.h) rely on to operate correctly?

null terminating character

500

Write a single, valid GCC command to compile two C files named 'main.c' and 'helper.c' into a single executable named 'app', with debugging and all warnings enabled.

gcc -Wall -g main.c helper.c -o app

500

Can you "run" an executable that contains a single function called "main()", and

Can you "run" an executable that does not contain a function called "main()"?

 

Yes and No
500

Write a function that defines a structure, initializes it, writes   it to a file called "struct.out", closes the file, re-opens the file   for read-only access, reads a single structure into a new struct   variable and then closes the file.  Print the structure contents to   the screen.  On any error, return -1.  Otherwise return 0.

#include <stdio.h>

#include <stdlib.h>

#include <string.h>


typedef struct {

    char name[50];

    int age;

} Person;


int processPersonStructure() {

    Person person1 = {"John Doe", 30}; // Initializing the structure

    FILE *file;


    // Step 1: Write the structure to a file

    file = fopen("struct.out", "wb");

    if (!file) {

        perror("Failed to open file for writing");

        return -1;

    }

    if (fwrite(&person1, sizeof(Person), 1, file) != 1) {

        perror("Failed to write structure to file");

        fclose(file);

        return -1;

    }

    fclose(file); // Close the file after writing


    // Step 2: Re-open the file for read-only access and read the structure

    file = fopen("struct.out", "rb");

    if (!file) {

        perror("Failed to open file for reading");

        return -1;

    }


    Person person2; // Create a new structure variable to read into

    if (fread(&person2, sizeof(Person), 1, file) != 1) {

        perror("Failed to read structure from file");

        fclose(file);

        return -1;

    }

    fclose(file); // Close the file after reading


    // Step 3: Print the contents of the read structure

    printf("Name: %s\nAge: %d\n", person2.name, person2.age);


    return 0; // Success

}


int main() {

    if (processPersonStructure() == -1) {

        printf("An error occurred.\n");

    } else {

        printf("Structure processed successfully.\n");

    }

    return 0;

}


500

And assume that p is initialized to point to one of the integers in arr.   Which of the following statements are legitimate?  Why or why not?      

1) p = arr;      2) arr = p;      3) p = &arr[2];   4) p = arr[x];    5) p = &arr[x];  6) arr[x] = p;   7) arr[p] = x;   8) &arr[x] = p;   9) p = &arr;      10) x = *arr;     11) x = arr + x;  12) p = arr + x;  13) arr = p + x;   14) x = &(arr+x);   15) p++;      16) x = --p;      17) x = *p++;     18) x = (*p)++;    19) arr++;    20) x = p - arr;    21)  x = (p>arr);  22) arr[*p]=*p;  23) *p++ = x;    24)  p = p + 1;    25)  arr = arr + 1;

1) T

2) F

3) T

4) F

5) T

6) F

7) F

8) F

9) F

10) T

11) F

12) T

13) F

14) F

15) T

16) F

17) T

18) T

19) F

20) T

21) T

22) T

23) T

24) T

25) F