1/182
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
What is system-level file I/O in C?
Low-level file input/output that uses file descriptors and system calls like open, read, write, and close instead of fopenand fprintf.
What is a file descriptor?
A small integer returned by the OS that identifies an open file.0 = stdin, 1 = stdout, 2 = stderr, 3+ = user files.
What does the open() system call do?
Opens a file and returns a file descriptor.Example: int fd = open("file.txt", O_RDONLY);
What header files are needed for system I/O?
#include <fcntl.h>
#include <unistd.h>
What flags can be used with open()?
O_RDONLY - read only
O_WRONLY - write only
O_RDWR - read/write
O_CREAT - create file if it doesn't exist
O_TRUNC - truncate file to 0 bytes
What does the third parameter in open() mean (e.g., 0644)?
File permission bits (read/write for owner, read for group/others).
What does read() do?
Reads bytes from a file descriptor into a buffer.Example: read(fd, buffer, sizeof(buffer));
What does write() do?
Writes bytes from a buffer to a file descriptor.Example: write(fd, msg, strlen(msg));
What does close() do?
Closes an open file descriptor and frees it for reuse.Example: close(fd);
What does lseek() do?
Moves the file offset (like a cursor).Example: lseek(fd, 0, SEEK_SET); moves to the start.
What are SEEK_SET, SEEK_CUR, and SEEK_END used for?
Positioning the file pointer: beginning, current position, or end.
What happens if open() fails?
Returns -1, and errno is set to indicate the error.
What are some features with system i/o?
File descriptiors, non(unbuffered), slower for small ops, low-level OS work
What are some features with standard io?
FILE*, Bufferred, faster, regular file reading/writing
What is standard file I/O in C?
It's a higher-level way to handle files using the C standard library (`stdio.h`), providing buffering and functions like `fopen`, `fclose`, `fread`, `fprintf`, etc.
What header file is required for standard file I/O?
#include <stdio.h>
What is a FILE pointer in C?
A pointer to a structure (`FILE *`) that represents an open file and stores information about it (mode, buffer, position, etc.).
What are the common fopen() modes?
"r" = read, "w" = write (truncates), "a" = append, "r+" = read/update, "w+" = write/read (truncates), "a+" = append/read.
What does fclose() do?
Closes an open file. Example: fclose(fp);
What does fprintf() do?
Writes formatted text to a file (like printf but with a FILE). Example: fprintf(fp, "Age: %d\n", age);
What does fscanf() do?
Reads formatted input from a file (like scanf but with a FILE). Example: fscanf(fp, "%d", &x);
What does fgetc() do?
Reads a single character from a file. Returns the character (as int) or EOF.
What does fputc() do?
Writes a single character to a file. Example: fputc('A', fp);
What does fgets() do?
Reads a line (string) from a file until newline or EOF. Example: fgets(buffer, 100, fp);
What is EOF in C file I/O?
End Of File marker, returned by reading functions when no more data is available.
What does feof() do?
Checks if the end of file has been reached. Example: while(!feof(fp)) { ... }
What does fread() do?
Reads binary data from a file. Example: fread(buffer, size, count, fp);
What does fwrite() do?
Writes binary data to a file. Example: fwrite(buffer, size, count, fp);
What does fseek() do?
Moves the file pointer to a specific location in the file. Example: fseek(fp, 0, SEEK_SET);
What do SEEK_SET, SEEK_CUR, and SEEK_END mean?
Constants for fseek(): start of file, current position, and end of file.
What does ftell() do?
Returns the current position of the file pointer. Example: long pos = ftell(fp);
What does rewind() do?
Moves the file pointer back to the start of the file.
What's the difference between fopen and open?
fopen uses FILE* and buffering (high-level); open uses file descriptors (low-level, unbuffered).
What's the main advantage of standard I/O?
Automatic buffering and convenience for formatted text operations.
What's the main disadvantage of standard I/O?
Slower for small, low-level operations compared to unbuffered system I/O.
What are the main types of errors in C?
Syntax errors, runtime errors, and logical errors.
What is a syntax error?
An error in the structure or grammar of the code (e.g., missing semicolon, undeclared variable).
When are syntax errors detected?
At compile time — the compiler stops and reports the issue.
What is a runtime error?
An error that occurs while the program is running (e.g., dividing by zero, invalid memory access).
When are runtime errors detected?
During execution — the program compiles but crashes or behaves unexpectedly.
What is a logical error?
The program runs without crashing but produces incorrect results due to faulty logic.
When are logical errors detected?
By testing and debugging — the compiler won't catch them.
What are common runtime errors in C?
Segmentation fault, divide by zero, invalid pointer dereference, file not found, memory leaks.
What causes a segmentation fault?
Accessing memory that doesn't belong to your process (like dereferencing a NULL or invalid pointer).
What does errno represent in C?
A global variable set by system calls and library functions when an error occurs.
What header is needed to use errno?
#include
How can you print a human-readable error message from errno?
Use perror("message") or strerror(errno).
What does perror() do?
Prints an error message for the last system or library error to stderr.
Example of using perror()
FILE *fp = fopen("missing.txt", "r"); if (!fp) perror("File open failed");
What does strerror() do?
Converts an error code (like errno) into a human-readable string.
Example of using strerror()
FILE *fp = fopen("missing.txt", "r"); if (!fp) printf("Error: %s\n", strerror(errno));
What is debugging?
The process of finding and fixing errors or bugs in a program.
What tool is commonly used to debug C programs?
gdb (GNU Debugger)
What compiler flag should you use to enable debugging info?
`-g` flag in gcc (example: gcc -g program.c -o program)
How do you start debugging with gdb?
`gdb ./program`
What command in gdb runs the program?
run
What does a breakpoint do in gdb?
Stops execution at a specific line or function to inspect program state.
How to set a breakpoint in gdb?
break
How to view variable values in gdb?
print
How to step line by line in gdb?
step (enters functions) or next (skips over functions)
What does backtrace show in gdb?
Shows the call stack (which functions were called before a crash).
How can you check for memory leaks in C?
Use Valgrind: `valgrind ./program`
What does Valgrind do?
Detects memory leaks, invalid reads/writes, and uninitialized memory usage.
How can you use compiler warnings to find bugs?
Compile with flags like `-Wall -Wextra` to show more potential issues.
What's the difference between a warning and an error?
Warnings don't stop compilation but suggest potential issues; errors stop the compiler.
What is assert() used for?
Used to test assumptions in code; if false, it stops the program.
What header is needed for assert()?
#include
Example of using assert()
int x = 5; assert(x > 0);
Why is assert() helpful?
Helps catch logic errors early during development and testing.
What are the main memory areas used by a C program?
Code (text) segment, data segment, heap, and stack.
What is stored in the code (text) segment?
The compiled machine instructions (the actual program code).
What is stored in the data segment?
Global and static variables — split into initialized and uninitialized parts.
What is the heap used for?
Dynamically allocated memory (allocated at runtime using malloc, calloc, realloc).
What is the stack used for?
Function calls, local variables, and function parameters — grows and shrinks as functions are called and return.
When is memory allocated on the stack?
Automatically when a function is called.
When is memory released from the stack?
Automatically when the function returns.
When is heap memory allocated?
At runtime, manually using functions like malloc().
When must heap memory be freed?
Manually using free() to prevent memory leaks.
What is static memory allocation?
Memory is assigned at compile time (e.g., global/static variables).
What is dynamic memory allocation?
Memory is assigned at runtime (e.g., malloc, calloc, realloc).
What header file is required for dynamic memory allocation?
#include
What does malloc() do?
Allocates a block of memory of a given size and returns a pointer to it.
Example of malloc() usage
int arr = (int) malloc(5 * sizeof(int));
What does calloc() do?
Allocates memory for multiple elements and initializes them to zero.
Example of calloc() usage
int arr = (int) calloc(5, sizeof(int));
What does realloc() do?
Resizes an existing memory block previously allocated by malloc or calloc.
Example of realloc() usage
arr = realloc(arr, 10 * sizeof(int));
What does free() do?
Deallocates memory previously allocated on the heap to prevent memory leaks.
Example of freeing memory
free(arr);
What is a memory leak?
When allocated memory is never freed, causing loss of available memory over time.
What is a dangling pointer?
A pointer that refers to memory that has been freed or is invalid.
How to avoid dangling pointers?
Set pointers to NULL after freeing memory.
What is a NULL pointer?
A pointer that doesn't point to any valid memory address (used to indicate "nothing").
What happens if you dereference a NULL pointer?
Usually causes a segmentation fault (crash).
What are segmentation faults caused by?
Accessing invalid memory (like dereferencing a NULL or uninitialized pointer).
What is pointer arithmetic?
Adding or subtracting integers from pointers to navigate through memory.
What is sizeof used for in memory management?
Returns the number of bytes required for a data type or variable.
Example of sizeof usage
printf("%zu", sizeof(int));
What is the lifetime of a stack variable?
Exists only while the function it's in is executing.
What is the lifetime of a static variable?
Exists for the entire duration of the program (even if declared inside a function).