C Standard Library

Beginner
⏱️ 15 min read
📚 Updated: Jul 2026
🎯 19 Headers
#include <*.h>

What You’ll Learn

The C standard library is a toolbox of headers shipped with every C compiler—<stdio.h> for printing, <string.h> for text, <stdlib.h> for memory, and more. This page is your central hub: understand how #include works, browse all 19 header tutorials, and try examples that combine the most common libraries.

01

#include

Pull in headers.

02

stdio.h

printf, files.

03

string.h

Text in C.

04

stdlib.h

malloc, exit.

05

Header Index

19 tutorials.

06

-lm

math.h link tip.

Definition and Usage

A header file (.h) declares functions, types, and macros. The library implementation (linked automatically on most systems) provides the machine code. You write #include <stdio.h> so the compiler knows printf exists; the linker attaches the real printf from the C runtime.

The C standard defines many headers. This site documents 19 of the most-used ones—each with its own tutorial (syntax, five examples, FAQs). Start here for the big picture, then dive into individual headers from the index below.

💡
Beginner Tip

Include only what you need in each .c file. If a file uses printf and strlen, include both <stdio.h> and <string.h>. Do not rely on one header to include another unless the standard guarantees it.

📝 Syntax

Include standard headers with angle brackets at the top of your source file:

C
#include <stdio.h>   /* printf, scanf, fopen */
#include <stdlib.h>  /* malloc, exit, atoi */
#include <string.h>  /* strlen, strcpy, strcmp */

Most common headers for beginners

  • <stdio.h> — input and output (printf, fgets, files)
  • <stdlib.h> — memory, conversion, exit, rand
  • <string.h> — C strings (strlen, strcmp, strcpy)
  • <ctype.h> — test characters (isdigit, tolower)
  • <math.h>sqrt, sin, pow (often needs -lm)

Compile and link

C
gcc main.c -std=c11 -o main
gcc main.c -std=c11 -o main -lm    /* if you use math.h */

⚡ Quick Reference

HeaderBeginner picks
stdio.hprintf, scanf, fopen, fclose
stdlib.hmalloc, free, exit, atoi
string.hstrlen, strcmp, strcpy, memcpy
ctype.hisalpha, isdigit, tolower
math.hsqrt, pow, fabs
time.htime, strftime, difftime

Standard Library Header Index

Search by header name or browse by category. Every card links to a full tutorial with examples and FAQs.

Diagnostics & Errors

2 headers

Catch bugs during development and read system error codes when calls fail.

Types & Limits

5 headers

Sizes, booleans, fixed-width integers, and floating-point constants.

Input/Output & Strings

2 headers

Console and file I/O plus null-terminated string manipulation.

Memory & Utilities

2 headers

Heap allocation, program exit, random numbers, and variable arguments.

Characters & Locale

4 headers

Narrow and wide character tests, locales, and international text.

Math & Time

2 headers

Floating-point math and calendar/CPU time.

Advanced Control

2 headers

Non-local jumps and asynchronous signal handling.

Examples Gallery

Five small programs that combine the most common standard library headers. Compile with gcc file.c -std=c11 -o out.

📚 Getting Started

Output and string basics from the reference example, improved.

Example 1 — Hello with stdio.h

The smallest useful program uses printf from <stdio.h>.

C
#include <stdio.h>

int main(void) {
    printf("Hello from the C standard library!\n");
    printf("stdio.h provides printf, scanf, and file I/O.\n");
    return 0;
}

How It Works

printf writes formatted text to stdout. \n starts a new line. return 0; tells the operating system the program succeeded.

Example 2 — String Compare and Concatenate

From the reference: strcmp and strcat with stdio + string.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    char part1[32] = "codetofun";
    char part2[] = ".com";
    char domain[48];

    strcpy(domain, part1);

    if (strcmp(part1, part2) == 0) {
        printf("Strings are equal.\n");
    } else {
        printf("'%s' and '%s' differ.\n", part1, part2);
    }

    strcat(domain, part2);
    printf("Full domain: %s\n", domain);
    printf("Length: %zu\n", strlen(domain));

    return 0;
}

How It Works

strcmp returns 0 when strings match. strcat appends to an existing C string—the buffer must be large enough. Use %zu with strlen (returns size_t).

📈 Practical Patterns

Memory, character tests, and combining headers.

Example 3 — Dynamic Array with stdlib.h

malloc and free from <stdlib.h> allocate heap memory.

C
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    size_t n = 5;
    size_t i;
    int *nums = malloc(n * sizeof *nums);

    if (nums == NULL) {
        fprintf(stderr, "malloc failed\n");
        return 1;
    }

    for (i = 0; i < n; i++) {
        nums[i] = (int)(i * 10);
    }

    printf("Dynamic array: ");
    for (i = 0; i < n; i++) {
        printf("%d ", nums[i]);
    }
    printf("\n");

    free(nums);
    return 0;
}

How It Works

Stack arrays have fixed size at compile time. malloc requests bytes at runtime. Always check for NULL and call free when done.

Example 4 — Count Digits with ctype.h

Scan a C string and count digits using isdigit.

C
#include <ctype.h>
#include <stdio.h>
#include <string.h>

int main(void) {
    const char *text = "Room 42, Building 7";
    size_t i;
    size_t digits = 0;

    for (i = 0; i < strlen(text); i++) {
        if (isdigit((unsigned char)text[i])) {
            digits++;
        }
    }

    printf("Text: %s\n", text);
    printf("Digit characters: %zu\n", digits);

    return 0;
}

How It Works

ctype.h functions expect unsigned char or EOF cast for safety. isdigit tests each byte of a narrow string.

Example 5 — Timestamped Log Line

Combine stdio, string, stdlib, and time like a tiny real app.

C
#include <stdio.h>
#include <string.h>
#include <time.h>

int main(void) {
    char buffer[64];
    char message[] = "User logged in";
    time_t now = time(NULL);
    struct tm *t = localtime(&now);

    if (t == NULL) {
        return 1;
    }

    strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", t);
    printf("[%s] %s (len %zu)\n", buffer, message, strlen(message));

    return 0;
}

How It Works

Real programs mix headers: time.h for the clock, string.h for length, stdio.h for output. Each header adds a focused set of tools.

🚀 Common Use Cases

  • Console appsstdio.h for user interaction.
  • File toolsfopen, fread, fprintf.
  • Text processingstring.h for parsing and building strings.
  • Dynamic datastdlib.h malloc/realloc.
  • Math & gamesmath.h, stdlib.h rand.
  • International appswchar.h, wctype.h, locale.h.

🧠 How the Standard Library Fits Together

1

#include headers

Compiler sees declarations for functions and types.

Compile
2

Your code calls APIs

printf, malloc, strcmp, etc.

Program
3

Linker binds library

Implementation code from libc (and -lm for math).

Link
=

Portable C program

Same header names work across compilers that support standard C.

📝 Notes

  • Angle brackets <> for standard headers; quotes for your own .h files.
  • Not every function lives where you expect—abs is in stdlib.h, not math.h.
  • Check return values: mallocNULL, fopenNULL, scanf → item count.
  • errno from errno.h after failed system/library calls; use perror from stdio.h.
  • C99+ features (stdbool.h, stdint.h) need -std=c99 or later.
  • POSIX adds extra headers (not covered here)—stick to standard C for portability.

🚀 Usage Tips

  • Include headers explicitly — do not rely on transitive includes.
  • Read each tutorial — every header page has five examples and FAQs.
  • Handle errors — check return values; use errno and perror.
  • Link mathgcc ... -lm when using math.h.
  • Learn the sidebar order — tutorials chain from assert.h to wctype.h.

Conclusion

The C standard library is the foundation of practical C programming. Use this hub to pick the right header, then open the dedicated tutorial for deep coverage. Start with stdio.h and string.h, add stdlib.h for memory, and grow from there.

Browse the index above or follow the sidebar from assert.h through wctype.h.

💡 Best Practices

✅ Do

  • #include only headers you use in that file
  • Compile with -std=c11 or newer
  • Free every malloc with free
  • Use fgets instead of unbounded scanf("%s")
  • Read the per-header tutorial before heavy use

❌ Don’t

  • Assume headers include each other portably
  • Ignore compiler warnings about implicit declarations
  • Mix wide and narrow I/O without conversion
  • Use gets (removed from C11)
  • Forget -lm when linking math functions

Key Takeaways

Knowledge Unlocked

Five things to remember about the C library

Your map to every standard header on this site.

5
Core concepts
📝 02

stdio

I/O first.

Start
🔗 03

string

Text ops.

Core
🛠 04

stdlib

Heap mem.

malloc
📚 05

19 guides

Full index.

Hub

❓ Frequently Asked Questions

The C standard library is a set of headers (stdio.h, stdlib.h, string.h, and others) that declare functions, types, and macros for I/O, memory, strings, math, time, and more. You #include the headers you need; the linker connects your program to the library implementation.
Use angle brackets: #include <stdio.h>. Angle brackets tell the compiler to search system include paths. Quotes (#include "my.h") are for your own project headers. One .c file includes only the headers it directly uses.
Start with stdio.h (printf, scanf), string.h (strlen, strcmp), and stdlib.h (malloc, exit). Add ctype.h for character tests, math.h for calculations, and errno.h when file or system calls fail. This hub links to full tutorials for all 19 headers.
Usually no for stdio, stdlib, and string. On GCC and Clang, math.h functions often require -lm at the end: gcc program.c -std=c11 -o program -lm. Windows MSVC typically links math automatically.
The .h header declares names (prototypes, macros, types). The compiled library provides the actual machine code for those functions. Including stdio.h lets the compiler check printf; the linker pulls in printf's implementation.
Read the overview, browse the header index below, and try the five examples that combine stdio, string, and stdlib. Then open assert.h or stdio.h tutorials in the order shown in the sidebar.
Did you know?

Every C standard header is included with #include <name.h>. The compiler finds declarations; you link the C library automatically on most systems—except math.h often needs -lm on GCC/Clang.

Start Your First Header Tutorial

Open stdio.h for printf and scanf, or begin at assert.h in sidebar order.

stdio.h tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

19 people found this page helpful