C Standard Library Reference

POSIX and C99 standard library functions searchable by header and name.

Searchable C standard library reference organized by header — stdio.h, stdlib.h, string.h, math.h, ctype.h, time.h — with each function's prototype, purpose, and a usage note covering common pitfalls. It runs free in your browser on Gera Tools, with nothing uploaded.

Last updated Source: Gera Tools

What is the difference between malloc and calloc?

malloc(size) allocates uninitialized memory of the given byte count. calloc(n, size) allocates an array of n elements, zero-initializes the whole block, and checks for multiplication overflow internally. Use calloc when you need zeroed memory or are allocating an array.

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.hprintf, scanf, fopen, fgets, fread, fwrite, fprintf, snprintf, fclose.
  • stdlib.hmalloc, calloc, realloc, free, atoi, strtol, qsort, bsearch, rand, exit.
  • string.hstrlen, strcpy, strncpy, strcmp, strcat, memcpy, memmove, memset, strstr.
  • ctype.hisdigit, isalpha, isspace, toupper, tolower.
  • math.hsqrt, pow, floor, ceil, fabs, fmod.
  • time.htime, 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: fopen returns NULL, allocation functions return NULL, and many stdio calls return EOF. Always check.
  • strncpy does not guarantee a terminating '\0' if the source is exactly as long as the buffer — terminate manually.
  • Prefer strtol/strtod over atoi/atof: they report conversion errors, while the ato* family silently returns 0 on bad input.
  • Functions tagged POSIX (strdup pre-C23, getline) are not part of plain standard C.