█
LastWrite
  • > Curriculum
  • > Pricing
  • > For Educators
  • > About
  • > Contact
Log InGet Started

Questions, concerns, bug reports, or suggestions? We read every message, write to us at [email protected].

More ways to reach us →
LastWrite

Structured computer science lessons for aspiring developers and security professionals.

[email protected]

(201) 785-7951

Mon–Fri, 9 AM–5 PM EST

Learn

  • Curriculum
  • Pricing

Company

  • About
  • For Educators & Schools
  • Contact Us

Legal

  • Terms of Service
  • Privacy Policy
© 2026 LastWrite. All rights reserved.
Curriculum/Programming Languages/C/The C Standard Library
40 minIntermediate

The C Standard Library

After this lesson, you will be able to: Know what's in stdio.h, stdlib.h, string.h, math.h, time.h, ctype.h, so you don't reinvent.

C's standard library is small but capable. Knowing it prevents 300-line homebrew utility scripts.

Prerequisites:File I/O

The headers worth memorising

stdio.h: printf, fprintf, snprintf, fopen, fgets, fread, fclose, perror, ferror. stdlib.h: malloc, free, calloc, realloc, atoi, atof, exit, qsort, bsearch, rand, srand, getenv. string.h: strlen, strncpy, strcat, strcmp, strncmp, strchr, strstr, memcpy, memset, memcmp, memmove. math.h: sin, cos, sqrt, pow, log, ceil, floor, round, fabs. (Link with -lm on Linux.) time.h: time, localtime, strftime, clock, difftime. ctype.h: isdigit, isalpha, isspace, toupper, tolower.

Examples

Half a page covers the daily stdlib usage.

tsx
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
int main(void) {
// Parse int from string
int n = atoi("42");
// Safe formatted output to buffer
char buf[64];
snprintf(buf, sizeof(buf), "User #%d", 5);
// Compare
if (strcmp("abc", "abc") == 0) printf("equal\n");
// Char classification
for (char *p = "Hello123"; *p; p++) {
if (isdigit(*p)) printf("digit %c\n", *p);
}
// Time
time_t now = time(NULL);
char ts[64];
strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S", localtime(&now));
printf("Now: %s\n", ts);
// Sort
int nums[] = {3, 1, 4, 1, 5};
qsort(nums, 5, sizeof(int), (int (*)(const void *, const void *))strcmp); // wrong compare; just shape
return 0;
}

POSIX vs ISO C

ISO C is what's portable to ALL platforms (Windows + Unix). POSIX (unistd.h, sys/types.h, etc.) adds Unix-specific APIs: read, write, open, fork, exec, pipe. If you're writing portable code: stick to ISO C. If Unix only: POSIX gives more power.

Common mistakes only experienced C devs avoid

atoi without checking, silent 0 on bad input. Use strtol with errno checking. sprintf instead of snprintf. Buffer overflows. rand() for security. Not cryptographically secure. Use /dev/urandom or getrandom() on Linux. Forgetting `-lm` on Linux when using math.h.

Quick Check

Why use snprintf over sprintf?

Pick the security answer.

Sign in and purchase access to unlock this lesson.

Sign in to purchase
←File I/O
Back to C
Compilation and Linking→