← C roadmap

Topic 9 of 15 · Intermediate

Functions Reference: Standard Library

stdio, string, stdlib, math and ctype functions with their use.

Concept

C's standard library is split into headers. Include the right one before using a function: <stdio.h> for input/output, <string.h> for strings, <stdlib.h> for memory and conversions, <math.h> for maths, <ctype.h> for characters.

Many string functions don't check buffer sizes — prefer the n-versions (strncpy, snprintf) and always leave room for '\0'.

When using <math.h> with gcc, link the maths library with -lm.

Cheat sheet

Function (header)What it doesExample
printf / scanf (stdio)Formatted output / inputprintf("%d\n", x)
fgets(buf, n, stdin) (stdio)Read a line safelyfgets(name, 50, stdin)
puts / putchar / getchar (stdio)Simple output / inputputchar('A')
sprintf / snprintf (stdio)Format into a stringsnprintf(b, 20, "%d", n)
fopen / fclose (stdio)Open / close a filefopen("a.txt", "r")
fprintf / fscanf (stdio)Formatted file I/Ofprintf(fp, "%s", s)
fread / fwrite (stdio)Binary file I/Ofwrite(&s, sizeof s, 1, fp)
fseek / ftell / rewind (stdio)Move in a filefseek(fp, 0, SEEK_END)
strlen(s) (string)Length without '\0'strlen("abc") → 3
strcpy / strncpy (string)Copy a stringstrncpy(d, s, sizeof d - 1)
strcat / strncat (string)Append a stringstrcat(d, "!")
strcmp / strncmp (string)Compare (<0, 0, >0)strcmp(a, b) == 0
strchr / strrchr (string)Find a characterstrchr(s, '@')
strstr(s, sub) (string)Find a substringstrstr(s, "sql")
strtok(s, delim) (string)Split into tokensstrtok(line, ",")
memset / memcpy / memmove (string)Fill / copy raw memorymemset(arr, 0, sizeof arr)
malloc / calloc / realloc / free (stdlib)Heap memorymalloc(n * sizeof(int))
atoi / atof / strtol (stdlib)Text → numberstrtol(s, NULL, 10)
abs / labs (stdlib)Absolute value (integers)abs(-4) → 4
rand / srand (stdlib)Random numberssrand(time(NULL)); rand() % 6
qsort / bsearch (stdlib)Sort / binary search any typeqsort(a, n, sizeof a[0], cmp)
exit(code) (stdlib)End the programexit(EXIT_FAILURE)
sqrt / pow (math)Square root / powerpow(2, 10) → 1024.0
ceil / floor / round (math)Roundingceil(4.2) → 5.0
fabs / fmod (math)Float abs / remainderfmod(7.5, 2) → 1.5
log / log10 / exp (math)Logarithms, e^xlog10(1000) → 3.0
sin / cos / tan (math)Trigonometry (radians)sin(M_PI / 2) → 1.0
isdigit / isalpha / isalnum (ctype)Character testsisdigit('7') → true
isupper / islower / isspace (ctype)Case / space testsisspace(' ') → true
toupper / tolower (ctype)Change casetoupper('a') → 'A'
time / clock (time)Current time / CPU timeclock() / CLOCKS_PER_SEC

Code (C17)

#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
    char line[] = "asha,92,hyderabad";
    char *tok = strtok(line, ",");
    while (tok) {
        printf("[%s] ", tok);
        tok = strtok(NULL, ",");
    }

    char name[] = "ravi";
    name[0] = (char)toupper(name[0]);
    printf("\n%s has %zu letters\n", name, strlen(name));

    int marks = atoi("88");
    printf("sqrt=%.2f pow=%.0f\n", sqrt(marks), pow(2, 5));

    int a[] = {0};
    memset(a, 0, sizeof a);
    return 0;
}
/* gcc demo.c -o demo -lm */

Remember

  • strcmp returns 0 when strings are equal.
  • strtok changes the original string.
  • Link <math.h> functions with -lm.

Interview questions

memcpy vs memmove?

memcpy is undefined if source and destination overlap; memmove handles overlapping regions safely.

Why is strtol better than atoi?

strtol reports errors and where parsing stopped; atoi silently returns 0 for invalid input.

Practise C with the AI mentor

Lesson test

Take an 8-question test on “Functions Reference: Standard Library” — concepts, output prediction, bug spotting and interview questions.

Can't find a C topic you need for exams or placements? Request it and we'll add it.