354 words
2 minutes
glibc_stdio_h
2026-01-02

link#


  • This is glibc’s stdio.h header - the standard C I/O library interface. Let me explain the main function categories and their purposes:

1. File Operations#

FunctionPurpose
fopen()Opens a file and returns a FILE stream pointer
freopen()Reopens a file with a different mode or reassigns an existing stream
fclose()Closes a stream and flushes buffers
remove()Deletes a file
rename()Moves/renames a file
fdopen()Creates a FILE stream from an existing file descriptor

2. Character I/O#

FunctionPurpose
fgetc() / getc()Reads one character from a stream
fgets()Reads a line (up to newline) into a string buffer
getchar()Reads one character from stdin
fputc() / putc()Writes one character to a stream
fputs()Writes a string to a stream
putchar()Writes one character to stdout
ungetc()Pushes a character back into the input buffer

3. Formatted I/O - Output (printf family)#

FunctionPurpose
printf()Writes formatted output to stdout
fprintf()Writes formatted output to a stream
sprintf()Writes formatted output to a string buffer (unsafe - no bounds)
snprintf()Writes formatted output with size limit (safe)
asprintf() / vasprintf()Allocates buffer automatically via malloc
dprintf()Writes formatted output to a file descriptor

4. Formatted I/O - Input (scanf family)#

FunctionPurpose
scanf()Reads formatted input from stdin
fscanf()Reads formatted input from a stream
sscanf()Reads formatted input from a string

5. Block I/O#

FunctionPurpose
fread()Reads an array of elements (binary data)
fwrite()Writes an array of elements (binary data)

6. File Positioning#

FunctionPurpose
fseek()Sets file position (uses long)
fseeko()Sets file position (uses off_t for large files)
ftell() / ftello()Gets current file position
rewind()Resets position to start of file
fgetpos() / fsetpos()Saves/restores file position (handles large files)

7. Error Handling#

FunctionPurpose
feof()Checks if EOF was reached
ferror()Checks if an error occurred
clearerr()Clears error and EOF flags
perror()Prints error message describing current errno

8. Buffering Control#

FunctionPurpose
setbuf()Sets buffer (or makes unbuffered if NULL)
setvbuf()Sets buffering mode and size (_IOFBF, _IOLBF, _IONBF)

9. Temporary Files#

FunctionPurpose
tmpfile()Creates a temporary file (deleted on close)
tmpnam()Generates a unique temporary filename
tempnam()Generates temp filename with directory/prefix control

10. Stream Locking (POSIX threads)#

FunctionPurpose
flockfile()Acquires exclusive lock on a stream
ftrylockfile()Tries to acquire lock without blocking
funlockfile()Releases lock on a stream

11. Line-Oriented Input#

FunctionPurpose
getline()Reads an entire line (allocates/reallocs buffer automatically)
getdelim()Reads up to a specified delimiter

12. Process Pipes#

FunctionPurpose
popen()Creates a pipe to/from a child process
pclose()Closes a pipe and waits for child process

Key Constants#

ConstantValue/Description
EOFEnd-of-file indicator (-1)
BUFSIZDefault buffer size (8192)
FILENAME_MAXMaximum filename length
FOPEN_MAXMaximum open streams (16)
SEEK_SET/CUR/ENDfseek origins
_IOFBF/_IOLBF/_IONBFBuffering modes

Important Features#

  • Large File Support: fopen64, fseeko64, off64_t for files >2GB
  • Thread Safety: _unlocked variants avoid locking overhead in single-threaded code
  • Cancellation Points: Many functions can be cancelled by POSIX threads
  • GNU Extensions: renameat2, open_memstream, obstack_printf
glibc_stdio_h
https://younghakim7.github.io/blog/posts/glibc_stdio_h/
Author
YoungHa
Published at
2026-01-02