Loading...

C essentialsFiles & System

Standard library tour: stdlib, math, time & ctype

10 min lesson

Next
  • The C standard library is a large set of ready-made functions that come with every C compiler, grouped into headers like stdlib.h, math.h, time.h and ctype.h.
  • Knowing the library matters because you rarely need to write common utilities yourself: converting text to numbers, computing square roots, getting the current time, or checking whether a character is a letter.
  • Think of the standard library as a well-stocked toolbox: each header is a drawer, and each function is a labeled tool you can pull out when you need it.
  • stdlib.h provides general utilities: atoi and strtol convert text to numbers, rand and srand generate pseudo-random numbers, malloc and free manage memory, and exit ends the program.
  • math.h provides math functions: sqrt, pow, fabs, sin, cos, floor and ceil, and constants like M_PI (with _USE_MATH_DEFINES or similar on some systems).
  • time.h provides time and clock functions: time gives the current time, localtime breaks it into fields, and clock measures CPU time.
  • ctype.h provides character checks: isalpha, isdigit, isspace, toupper and tolower, which are safer than manual range checks.
  • The computer links these functions from the standard library automatically; the only extra step is sometimes adding -lm when using math.h with gcc.
  • The syntax varies per function, but most follow the pattern returnType name(args);, like double sqrt(double x);.
  • A realistic use: converting a command-line string to a number with strtol, computing a square root with sqrt, and uppercasing a letter with toupper.
  • A surprising detail: atoi cannot report errors; strtol can, which is why strtol is preferred for serious input handling.
  • The example program uses strtol to parse a number, sqrt to compute its square root, and toupper to capitalize a letter, all in one short program.
Keep in mind
  • Learn to skim the manual for each header; the standard library saves you enormous time.
  • Prefer strtol over atoi when you need error detection.
  • Remember to add -lm on gcc when using math functions.
  • ctype functions are safer than manual checks for letters and digits.
  • The standard library is your friend. Explore it with confidence!
main.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>

int main(void) {
    // A string that looks like a number, as if typed on a command line
    const char *text = "144";
    // strtol parses the number and reports where parsing stopped
    char *end = NULL;
    long value = strtol(text, &end, 10);
    // Check that the whole string was a number
    if (*end != '\0') {
        printf("Not a number: %s\n", text);
        return 1;
    }
    // Print the parsed value
    printf("Parsed: %ld\n", value);

    // Use sqrt from math.h to compute the square root
    double root = sqrt((double)value);
    printf("Square root: %.1f\n", root);

    // Use toupper from ctype.h to capitalize a letter
    char letter = 'c';
    printf("Upper: %c\n", toupper((unsigned char)letter));

    return 0;
}

What happens: Parsed: 144 Square root: 12.0 Upper: C

Breaking it down

strtol call

strtol(text, &end, 10)

Parses the string as a base-10 long. The end pointer tells you where parsing stopped, so you can detect invalid input.

End check

if (*end != '\0')

Confirms the whole string was consumed. If there is leftover text, it was not a pure number.

sqrt

sqrt((double)value)

Computes the square root from math.h. The cast to double matches the function signature.

toupper

toupper((unsigned char)letter)

Converts a letter to uppercase. The cast to unsigned char avoids undefined behavior for negative char values.

Standard library headers

HeaderWhat it providesExample function
stdio.hInput and outputprintf, fopen
stdlib.hGeneral utilitiesstrtol, malloc
math.hMath functionssqrt, pow
time.hTime and clocktime, clock
ctype.hCharacter checksisalpha, toupper
string.hString functionsstrlen, strcmp

A helpful analogy

The standard library is a toolbox with labeled drawers. stdlib.h is the general tools drawer, math.h is the math drawer, time.h is the clock drawer, and ctype.h is the character drawer. You do not build your own hammer; you open the right drawer and pick the tool.

Nesting rules

  • 1.Include the header for every library function you call.
  • 2.Add -lm on gcc when you use math.h functions like sqrt or pow.
  • 3.Prefer strtol over atoi when you need to detect bad input.
  • 4.Cast char to unsigned char before passing it to ctype functions to avoid undefined behavior.

Common mistakes

Forgetting -lm when linking math functions

Avoid

gcc main.c -o app // undefined reference to sqrt

Use instead

gcc main.c -o app -lm

On many systems the math library is separate. Without -lm the linker cannot find sqrt.

Using atoi without error checking

Avoid

int n = atoi("abc"); // returns 0, no error

Use instead

char *end; long n = strtol("abc", &end, 10);

atoi returns 0 for invalid input, which is indistinguishable from a real 0. strtol reports where parsing stopped.

Passing a negative char to toupper

Avoid

toupper(c) // c may be negative

Use instead

toupper((unsigned char)c)

ctype functions expect an unsigned char or EOF. A negative char is undefined behavior on some systems.

Quick tips

  • •Skim the man page or documentation for each header once; it saves hours later.
  • •Use rand and srand for quick pseudo-random numbers, but not for security.
  • •Prefer clock for measuring CPU time and time for wall-clock time.
  • •Try changing text to "abc" in the example to see the error path run.
  • •Remember to compile with -lm when using math functions on gcc.
Previous
Next