The C standard library is split across a handful of headers, each grouping related functions for I/O, memory, strings, math, and time. Calling the wrong one — or ignoring a return value — is a frequent source of bugs. This tool is a searchable reference covering each function’s prototype, purpose, and the pitfalls that bite C programmers.
How it works
Every function belongs to a header you must #include. The reference is grouped
by header:
stdio.h—printf,scanf,fopen,fgets,fread,fwrite,fprintf,snprintf,fclose.stdlib.h—malloc,calloc,realloc,free,atoi,strtol,qsort,bsearch,rand,exit.string.h—strlen,strcpy,strncpy,strcmp,strcat,memcpy,memmove,memset,strstr.ctype.h—isdigit,isalpha,isspace,toupper,tolower.math.h—sqrt,pow,floor,ceil,fabs,fmod.time.h—time,clock,strftime,localtime.
Worked example
#include <stdio.h>
#include <string.h>
char buf[16];
fgets(buf, sizeof buf, stdin); // bounded read — safe
buf[strcspn(buf, "\n")] = '\0'; // strip trailing newline
char dst[8];
snprintf(dst, sizeof dst, "%s", buf); // bounded copy, always NUL-terminated
fgets and snprintf take an explicit size, which is why they are preferred
over gets and strcpy.
Common pitfalls by header
stdio.h pitfalls
scanf with %s has no length limit — it overflows exactly like gets.
Use %15s (or whatever your buffer size minus one is) to bound the read.
printf with a user-supplied format string is a format-string vulnerability;
always use printf("%s", str) not printf(str).
fread and fwrite return the number of items transferred, not bytes.
Checking ferror after a short count distinguishes a real error from
end-of-file.
stdlib.h pitfalls
malloc returns NULL on allocation failure; dereferencing without checking
is undefined behaviour that often manifests as a crash on a memory-scarce
system, not during development. Always:
int *p = malloc(n * sizeof *p);
if (!p) { /* handle */ }
qsort comparator must return negative, zero, or positive — not just true/false.
Returning a - b for integers silently overflows for large values; use
(a > b) - (a < b) instead.
string.h pitfalls
strcpy and strcat are unsafe because they have no buffer-size parameter.
Replace them with snprintf for concatenation or strlcpy/strlcat on
platforms that provide them.
memcpy with overlapping regions is undefined. Use memmove when the regions
might overlap.
Notes
- Return values carry the error signal:
fopenreturnsNULL, allocation functions returnNULL, and many stdio calls returnEOF. Always check. strncpydoes not guarantee a terminating'\0'if the source is exactly as long as the buffer — terminate manually.- Prefer
strtol/strtodoveratoi/atof: they report conversion errors, while theato*family silently returns 0 on bad input. - Functions tagged POSIX (
strduppre-C23,getline) are not part of plain standard C.