This GCC flag tells the compiler to include debugging information in the executable, allowing GDB to map source lines to machine instructions.
What is -g?
This character marks the end of a string in C.
What is the null terminator ('\0')?
This C library must be included to use functions like fopen, fclose, fread, and fprintf.
What is <stdio.h>?
In C, this function from <stdlib.h> dynamically allocates a block of memory on the heap.
What is malloc?
When you pass a pointer to a function in C, changes made through the pointer affect the original data.
What is this type of function argument passing called?
What is pass-by-reference (simulated with pointers)?
This command compiles program.c into an executable called program using GCC, showing all compiler warnings during the build.
What is gcc -Wall -o program program.c?
In C, this library must be included to use functions like strlen, strcpy, and strcmp.
What is <string.h>?
When reading a file with fscanf or fgets, failing to check this return value can cause undefined behavior if the file ends or an error occurs.
What is the return value of fscanf or fgets (checking for EOF or NULL)?
This happens if you try to access an array index outside of its declared size, such as accessing arr[10] when arr[5] was declared.
What is undefined behavior?
What will this C code print?
int x = 5;
int *p = &x;
*p = *p + 1;
printf("%d\n", x);
What is 6?
(Explanation: *p = *p + 1 increments the value pointed to by p, which is x.)
When optimizing code for speed, this GCC flag is often used. However, if you compile with this flag alone without -g, debugging becomes harder because some variables may be optimized away.
What is -O (or -O2 and -O3)?
If you declare a string like char *s = "hello";, this part of memory holds the characters.
What is the read-only data section (or static memory)?
This function reads raw bytes from a file into a memory buffer, and you must correctly specify the size of each element and the number of elements to read.
Points are given only for the full function prototype!
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
In C, this function allocates memory and automatically initializes all bits to zero.
What is calloc?
What mistake does this C code make?
What is dereferencing an uninitialized pointer, causing undefined behavior?
When using GCC, this pair of flags would first compile a .c file into an object file without linking, and then link that object file into an executable. Name both flags in the order they are used.
What are -c and -o?
(Explanation: -c compiles to .o file without linking, -o names the output file.)
Given char str[10];, this function safely copies the string "cat" into str without overflowing it.
Points are given only for the entire function prootype!
What is strncpy(str, "cat", sizeof(str))?
If you want to move the file pointer to a specific byte offset in a file, you would use this function, often paired with ftell to keep track of position.
Points are only given for the full function prototype!
What is int fseek(FILE *stream, long int offset, int whence);?
You dynamically allocate a 2D array in C using double pointers. What is the correct two-step process?
What is:
Allocate an array of pointers with malloc,
Then allocate each row individually with malloc or calloc.
int arr[] = {1, 2, 3, 4};
int *p = arr;
printf("%d\n", *(p + 2));
What is 3?
When running GDB, this command lets you execute your program line-by-line, stopping after each source line so you can watch the program’s behavior closely.
What is step?
Suppose you want to manually copy one string into another using a while loop without using strcpy.
This specific condition should be checked in the loop to ensure the copy is done safely.
What is checking if the current character is not the null terminator (while (*src != '\0'))?
In C, if you open a file with fopen("data.txt", "r") and then immediately call fwrite on it without reopening, what happens, and why?
What is undefined behavior because the file was opened in read-only mode, not write mode?
What mistake does this C code make?
What is failing to allocate enough bytes?
(Explanation: malloc(5) only allocates 5 bytes, not 5 integers. It should be malloc(5 * sizeof(int)).)
What's wrong here?
int* createArray() {
int arr[5];
for (int i = 0; i < 5; i++) {
arr[i] = i * i;
}
return arr;
}
What is that arr is a local array on the stack, and returning its address causes undefined behavior because the memory is invalid after the function returns?