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 does | Example |
|---|---|---|
| printf / scanf (stdio) | Formatted output / input | printf("%d\n", x) |
| fgets(buf, n, stdin) (stdio) | Read a line safely | fgets(name, 50, stdin) |
| puts / putchar / getchar (stdio) | Simple output / input | putchar('A') |
| sprintf / snprintf (stdio) | Format into a string | snprintf(b, 20, "%d", n) |
| fopen / fclose (stdio) | Open / close a file | fopen("a.txt", "r") |
| fprintf / fscanf (stdio) | Formatted file I/O | fprintf(fp, "%s", s) |
| fread / fwrite (stdio) | Binary file I/O | fwrite(&s, sizeof s, 1, fp) |
| fseek / ftell / rewind (stdio) | Move in a file | fseek(fp, 0, SEEK_END) |
| strlen(s) (string) | Length without '\0' | strlen("abc") → 3 |
| strcpy / strncpy (string) | Copy a string | strncpy(d, s, sizeof d - 1) |
| strcat / strncat (string) | Append a string | strcat(d, "!") |
| strcmp / strncmp (string) | Compare (<0, 0, >0) | strcmp(a, b) == 0 |
| strchr / strrchr (string) | Find a character | strchr(s, '@') |
| strstr(s, sub) (string) | Find a substring | strstr(s, "sql") |
| strtok(s, delim) (string) | Split into tokens | strtok(line, ",") |
| memset / memcpy / memmove (string) | Fill / copy raw memory | memset(arr, 0, sizeof arr) |
| malloc / calloc / realloc / free (stdlib) | Heap memory | malloc(n * sizeof(int)) |
| atoi / atof / strtol (stdlib) | Text → number | strtol(s, NULL, 10) |
| abs / labs (stdlib) | Absolute value (integers) | abs(-4) → 4 |
| rand / srand (stdlib) | Random numbers | srand(time(NULL)); rand() % 6 |
| qsort / bsearch (stdlib) | Sort / binary search any type | qsort(a, n, sizeof a[0], cmp) |
| exit(code) (stdlib) | End the program | exit(EXIT_FAILURE) |
| sqrt / pow (math) | Square root / power | pow(2, 10) → 1024.0 |
| ceil / floor / round (math) | Rounding | ceil(4.2) → 5.0 |
| fabs / fmod (math) | Float abs / remainder | fmod(7.5, 2) → 1.5 |
| log / log10 / exp (math) | Logarithms, e^x | log10(1000) → 3.0 |
| sin / cos / tan (math) | Trigonometry (radians) | sin(M_PI / 2) → 1.0 |
| isdigit / isalpha / isalnum (ctype) | Character tests | isdigit('7') → true |
| isupper / islower / isspace (ctype) | Case / space tests | isspace(' ') → true |
| toupper / tolower (ctype) | Change case | toupper('a') → 'A' |
| time / clock (time) | Current time / CPU time | clock() / 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.
Lesson test
Take an 8-question test on “Functions Reference: Standard Library” — concepts, output prediction, bug spotting and interview questions.