Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 8 of 10

The C Standard Library - What's Available

The C standard library is a collection of header files and functions standardised by ISO C. It covers I/O (`stdio.h`), memory (`stdlib.h`, `string.h`), mathematics (`math.h`), character classification (`ctype.h`), and more. Knowing what the standard library provides prevents you from reinventing the wheel - and knowing its limits prevents misuse.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Key Standard Library Headers

Key Standard Library Headers
#include <stdio.h>    /* printf, scanf, fopen, fclose, fprintf, fgets */
#include <stdlib.h>   /* malloc, calloc, realloc, free, exit, atoi, rand */
#include <string.h>   /* strlen, strcpy, strncpy, strcmp, memcpy, memset */
#include <math.h>     /* sqrt, pow, sin, cos, floor, ceil (link with -lm) */
#include <ctype.h>    /* isdigit, isalpha, isspace, toupper, tolower */
#include <stdint.h>   /* int8_t, uint32_t, int64_t, SIZE_MAX */
#include <stdbool.h>  /* bool, true, false (C99) */
#include <limits.h>   /* INT_MAX, CHAR_MIN, UINT_MAX */
#include <float.h>    /* FLT_MAX, DBL_EPSILON */
#include <assert.h>   /* assert() - crash with a message if condition is false */
#include <errno.h>    /* errno, EINVAL, ENOMEM, ENOENT */
#include <time.h>     /* time_t, clock(), difftime(), strftime() */

/* ── Quick examples ──────────────────────────────────────── */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int main(void) {
    /* ctype.h - character classification */
    char ch = 'a';
    printf("isalpha('a')=%d  toupper='%c'
", isalpha(ch), toupper(ch));

    /* string.h - string utilities */
    char buf[32];
    strncpy(buf, "hello", sizeof(buf) - 1);
    buf[sizeof(buf) - 1] = '';   /* always terminate manually */
    printf("length=%zu
", strlen(buf));

    /* stdlib.h - string to number */
    const char *numstr = "42";
    int n = atoi(numstr);  /* NOTE: atoi gives 0 on error - use strtol for robust parsing */
    printf("atoi("%s") = %d
", numstr, n);

    /* stdlib.h - random numbers */
    srand(42);               /* seed the generator */
    printf("rand mod 10 = %d
", rand() % 10);

    /* assert.h - defensive programming */
    int count = 5;
    /* assert(count > 0); - no-op when NDEBUG defined, crash otherwise */
    (void)count;

    return 0;
}

Common Mistakes

  • Forgetting `-lm` when using `<math.h>` - on Linux, `gcc prog.c -o prog` will fail with 'undefined reference to sqrt'. Add `-lm`: `gcc prog.c -o prog -lm`.
  • Using `atoi` for production code - `atoi("abc")` silently returns 0 with no way to detect the error. Use `strtol` instead, which sets `errno` and provides an end-pointer to detect non-numeric input.
  • Not knowing `string.h` functions - manually writing `strlen`, `strcpy`, or `strcat` when the standard library already has them is error-prone. Know what's available and use it.

Tip

Tip

Practice The C Standard Library What in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Process virtual address space organizes code, static variables, heap, and call stack

Practice Task

Note

Practice Task - (1) Write a working example of The C Standard Library What from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with The C Standard Library What is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.

Key Takeaways

  • The C standard library is a collection of header files and functions standardised by ISO C.
  • Forgetting `-lm` when using `<math.h>` - on Linux, `gcc prog.c -o prog` will fail with 'undefined reference to sqrt'. Add `-lm`: `gcc prog.c -o prog -lm`.
  • Using `atoi` for production code - `atoi("abc")` silently returns 0 with no way to detect the error. Use `strtol` instead, which sets `errno` and provides an end-pointer to detect non-numeric input.
  • Not knowing `string.h` functions - manually writing `strlen`, `strcpy`, or `strcat` when the standard library already has them is error-prone. Know what's available and use it.

Topics in This Module

0/10 done