Files and the C Standard Library
File I/O, error handling and the standard headers every C programmer uses.
This is an overview — pick a sub-topic below.
Sub-topics (19)
File Handling in C
Open a file as a `FILE *` stream, read or write, then close it.
fopen and fclose
Open and close streams.
fread and fwrite
Read and write blocks of bytes — ideal for binary data.
fprintf and fscanf
Formatted output to and input from files — `printf`/`scanf` for streams.
fgets and fputs
Read a line safely into a buffer; write a string.
fseek and ftell
Move the file position and report where it is.
Binary Files
Store data as raw bytes instead of text — smaller and faster, but needs a defined format.
Error Handling in C
C has no exceptions: functions return error codes, NULL or set `errno`.
errno
A global (thread-local) error number set by library functions on failure.
The C Standard Library
The headers that come with every C compiler: I/O, strings, maths, memory, time and more.
assert.h
`assert(condition)` aborts with a message if a programmer assumption is false.
math.h
Floating-point maths: `sqrt`, `pow`, `sin`, `cos`, `atan2`, `fabs`, `floor`, `round`, `log`, `exp`.
stdlib.h
General utilities: memory (`malloc`, `free`), conversions (`strtol`, `strtod`), `qsort`, `bsearch`, `rand`, `exit`, `abs`.
stdint.h
Fixed-width integer types: `int8_t`, `uint16_t`, `int32_t`, `uint64_t`, plus `uintptr_t` and limits.
stdbool.h
Defines `bool`, `true` and `false` (built-in keywords since C23).
stdatomic.h
C11 atomic types and operations for lock-free shared data between threads or interrupts.
signal.h
Handle asynchronous signals like `SIGINT` (Ctrl+C).
time.h
Calendar time, clocks and formatting: `time`, `clock`, `difftime`, `strftime`, `localtime`.
locale.h
Localisation settings that affect number formatting and character classification.
Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.
Discussion (0)
Sign in or join free to ask questions and discuss this topic.