C Notes
Complete guide to the C Standard Library covering all header files, commonly used functions from stdio, stdlib, string, math, and more with practical examples.
The C Standard Library is a collection of header files that provide essential functionality for input/output, string manipulation, memory management, mathematics, and more. Every C compiler ships with these libraries, making them portable across platforms. Understanding the standard library means you don't need to reinvent the wheel for common tasks.
Complete List of C Standard Library Headers
C89/C90 Headers
| Header | Purpose |
|---|---|
<stdio.h> | Input/output (printf, scanf, file operations) |
<stdlib.h> | General utilities (malloc, atoi, rand, exit) |
<string.h> | String manipulation (strcpy, strcmp, memcpy) |
<math.h> | Mathematical functions (sin, cos, sqrt, pow) |
<ctype.h> | Character classification (isalpha, toupper) |
<time.h> | Date and time functions |
<limits.h> | Numeric limits (INT_MAX, CHAR_BIT) |
<float.h> | Floating-point limits |
<assert.h> | Diagnostic assertions |
<errno.h> | Error codes |
<signal.h> | Signal handling |
<setjmp.h> | Non-local jumps |
<stdarg.h> | Variable argument lists |
<stddef.h> | Common type definitions (size_t, NULL) |
<locale.h> | Localization |
C99 Additions
| Header | Purpose |
|---|---|
<stdbool.h> | Boolean type (bool, true, false) |
<stdint.h> | Fixed-width integer types (int32_t, uint8_t) |
<inttypes.h> | Format macros for fixed-width types |
<complex.h> | Complex number arithmetic |
<tgmath.h> | Type-generic math macros |
<fenv.h> | Floating-point environment |
C11 Additions
| Header | Purpose |
|---|---|
<stdalign.h> | Alignment specification |
<stdatomic.h> | Atomic operations |
<threads.h> | Thread support |
<stdnoreturn.h> | Noreturn function specifier |
stdio.h – Input/Output Functions
The most commonly used header in C. Handles console and file I/O.
Key stdio.h functions:
- Output:
printf,fprintf,sprintf,snprintf,puts,putchar - Input:
scanf,fscanf,fgets,getchar,fread - File:
fopen,fclose,fseek,ftell,rewind,fflush
stdlib.h – General Utilities
num=42, pi=3.14159, big=1000000 67 93 56 79 12 Sorted: 12 56 67 79 93 Found 67 at index 2 HOME: /home/user
string.h – String Manipulation
Length of str1: 5 After strcpy: Hello After strcat: Hello World strcmp: -1 strncmp: 0 Found 'World' at position: 6 Token: one Token: two Token: three
math.h – Mathematical Functions
#include <stdio.h>
#include <math.h> // Link with -lm
int main() {
// Basic operations
printf("sqrt(144) = %.0f\n", sqrt(144));
printf("pow(2, 10) = %.0f\n", pow(2, 10));
printf("fabs(-3.7) = %.1f\n", fabs(-3.7));
// Trigonometry
printf("sin(PI/2) = %.2f\n", sin(M_PI / 2));
printf("cos(0) = %.2f\n", cos(0));
// Rounding
printf("ceil(2.3) = %.0f\n", ceil(2.3));
printf("floor(2.7) = %.0f\n", floor(2.7));
printf("round(2.5) = %.0f\n", round(2.5));
// Logarithms
printf("log(e) = %.2f\n", log(M_E));
printf("log10(1000) = %.0f\n", log10(1000));
printf("log2(256) = %.0f\n", log2(256));
return 0;
}sqrt(144) = 12 pow(2, 10) = 1024 fabs(-3.7) = 3.7 sin(PI/2) = 1.00 cos(0) = 1.00 ceil(2.3) = 3 floor(2.7) = 2 round(2.5) = 3 log(e) = 1.00 log10(1000) = 3 log2(256) = 8
ctype.h – Character Classification
Original: Hello World 123! 'H' is alphabetic 'e' is alphabetic 'l' is alphabetic 'l' is alphabetic 'o' is alphabetic ' ' is space 'W' is alphabetic ... Uppercase: HELLO WORLD 123! Lowercase: hello world 123!
time.h – Date and Time
Timestamp: 1718280000 Date: 13/06/2025 Time: 14:30:00 Formatted: Friday, June 13, 2025 02:30 PM Computation took: 0.245 seconds
stdint.h – Fixed-Width Types
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
int main() {
int8_t byte_val = 127;
uint16_t port = 8080;
int32_t temperature = -40;
uint64_t file_size = 4294967296ULL; // > 4GB
printf("int8_t: %" PRId8 "\n", byte_val);
printf("uint16_t: %" PRIu16 "\n", port);
printf("int32_t: %" PRId32 "\n", temperature);
printf("uint64_t: %" PRIu64 "\n", file_size);
printf("\nSizes:\n");
printf("int8_t: %zu byte\n", sizeof(int8_t));
printf("int16_t: %zu bytes\n", sizeof(int16_t));
printf("int32_t: %zu bytes\n", sizeof(int32_t));
printf("int64_t: %zu bytes\n", sizeof(int64_t));
return 0;
}int8_t: 127 uint16_t: 8080 int32_t: -40 uint64_t: 4294967296 Sizes: int8_t: 1 byte int16_t: 2 bytes int32_t: 4 bytes int64_t: 8 bytes
Quick Reference: Most Used Functions
| Header | Essential Functions |
|---|---|
stdio.h | printf, scanf, fopen, fclose, fgets, fprintf, snprintf |
stdlib.h | malloc, calloc, free, realloc, atoi, qsort, exit, rand |
string.h | strlen, strcpy, strncpy, strcat, strcmp, strstr, memcpy, memset |
math.h | sqrt, pow, abs, ceil, floor, log, sin, cos |
ctype.h | isalpha, isdigit, isspace, toupper, tolower |
time.h | time, localtime, strftime, clock, difftime |
stdint.h | int8_t, int32_t, uint64_t, INT32_MAX |
stdbool.h | bool, true, false |
assert.h | assert |
errno.h | errno, perror, strerror |
Interview Questions on C Standard Library
Q1: What's the difference between malloc() and calloc()? malloc(size) allocates size bytes without initialization (contains garbage). calloc(n, size) allocates n*size bytes and initializes all to zero. calloc also checks for multiplication overflow.
Q2: Why is gets() dangerous and what should you use instead? gets() has no way to limit input length, causing buffer overflows. It was removed in C11. Use fgets(buf, size, stdin) instead.
Q3: What does errno do? errno is a global variable set by system calls and library functions to indicate what went wrong. Use strerror(errno) or perror("message") to get human-readable error descriptions.
Q4: How do you generate random numbers in a specific range? Use rand() % range + min. For example, numbers between 10 and 50: rand() % 41 + 10. Seed with srand(time(NULL)) for different sequences each run.
Q5: What's the difference between strncpy and strlcpy? strncpy doesn't guarantee null termination and pads with zeros. strlcpy always null-terminates and returns the source length for truncation detection. strlcpy is not standard C but is available on BSD/macOS.
Summary
The C Standard Library provides everything you need for most programming tasks without external dependencies. Master stdio.h for I/O, stdlib.h for memory and utilities, string.h for text processing, and math.h for calculations. Use stdint.h for portable integer types and stdbool.h for readable boolean logic. Knowing these libraries well means writing less code, fewer bugs, and more portable programs.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for C Standard Library Overview – All Headers and Common Functions.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, advanced, standard, library, c standard library overview – all headers and common functions
Related C Programming Topics