C Standard Library

Beginner
15 min read
Updated: Nov 2026
5 examples
19 headers

Definition and Usage

In a C tutorial, almost every useful program leans on the C standard library—headers such as <stdio.h>, <string.h>, and <stdlib.h>. A header declares functions, types, and macros; the library implementation supplies the machine code. You write #include <stdio.h> so the compiler knows printf exists; the linker attaches the real printf from the C runtime.

Header vs library

The .h file is the contract. The linked library (libc, and often libm for math) is the implementation. Missing an include is a compile problem; missing -lm for sqrt is a link problem.

What should beginners learn first?

Start with <stdio.h> (print and files), <string.h> (null-terminated text), and <stdlib.h> (malloc, conversion, exit). Then add <ctype.h> and <math.h> (often with -lm). Deeper tracks: C Strings and C Math.

This lesson vs single-header pages

This page is the C library lesson: how #include works, which header fits which job, and how headers work together. Each name in the headers index opens a focused guide (19 tutorials) with syntax, examples, and FAQs—for example stdio.h.

Include only what you use.

If a .c file needs printf and strlen, include both <stdio.h> and <string.h>. Do not rely on one header to pull in another unless the standard guarantees it.

Headers declare; libraries define.

A missing #include fails at compile time. A missing -lm for math fails at link time—different stage, different fix.

Angle brackets vs quotes.

#include <stdio.h> searches system paths. #include "my.h" is for your project headers. Use brackets for the standard library.

C Syntax

Put standard includes at the top of the source file, before your code:

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

int main(void) {
    printf("Hello, C library!\n");
    return 0;
}

Compile and link

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

Core C Concepts

How standard headers map to everyday C tutorial tasks:

HeaderBeginner picksTypical need
<stdio.h>printf, scanf, fopenConsole and file I/O
<string.h>strlen, strcmp, strcpyNull-terminated strings
<stdlib.h>malloc, free, exit, atoiHeap, conversion, exit
<ctype.h>isalpha, isdigit, tolowerCharacter classification
<math.h>sqrt, pow, fabsFloating-point math (-lm)
<time.h>time, strftime, difftimeClock and formatting

Header vs library vs linker

PieceWhat it does
#include <…>Gives the compiler declarations (prototypes, macros, types)
C runtime / libmProvides the actual function implementations
Linker flagsConnects your object file to those libraries (e.g. -lm)

Learning order in this C tutorial

StageFocus
1. I/O & stringsstdio.h, string.h
2. Memory & utilsstdlib.h, then stddef.h / stdint.h
3. Characters & mathctype.h, math.h, time.h
4. Diagnosticsassert.h, errno.h
5. Advancedstdarg.h, setjmp.h, signal.h, wide char headers

Choose a Header by Job

Not sure which tutorial to open? Match the C task to a header:

JobPrefer
Print / read / files<stdio.h>
Copy, compare, search text<string.h> — also C Strings
Heap, exit, atoi, rand<stdlib.h>
Test letters / digits / case<ctype.h>
sqrt, sin, pow, fabs<math.h> (+ -lm) — also C Math
Clock / format timestamps<time.h>
Debug checks / errno<assert.h>, <errno.h>
Fixed-width / bool / sizes<stdint.h>, <stdbool.h>, <stddef.h>

Jump to a category

How to Use the Standard Library in C

A practical workflow for this C tutorial track:

  1. List the jobs your program needs (print, read, copy a string, allocate memory, compute a root).
  2. Map each job to a header with Choose a Header by Job above.
  3. Add #include <…> lines at the top of the .c file—only what you use.
  4. Compile with -std=c11; add -lm last if you call math.h functions.
  5. Check return values (malloc, fopen) and free what you allocate.
  6. Open a single-header page from the searchable index when you need a full walkthrough.

Live Preview

Toggle the jobs you need—the preview shows which standard headers a C tutorial program would typically include.

Program needs
Suggested includes
#include <stdio.h> #include <string.h>

Compile: gcc main.c -std=c11 -o main

C Examples

Five starter patterns that combine the headers you meet first in a C tutorial. Compile with gcc file.c -std=c11 -o out.

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;
}
stdio.h Tutorial

How It Works

  1. #include pulls in the printf declaration.
  2. printf writes formatted text to stdout.
  3. \n ends the line.
  4. return 0 signals success to the OS.

Example 2 — Compare and concatenate with string.h

Mix stdio and string for everyday text work.

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;
}
string.h Tutorial

How It Works

  1. strcmp returns 0 when strings match.
  2. strcpy copies into a buffer you already sized.
  3. strcat appends—destination must have space.
  4. %zu prints strlen’s size_t result.

Example 3 — Dynamic array with stdlib.h

malloc and free allocate heap memory at runtime.

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;
}
stdlib.h Tutorial

How It Works

  1. malloc requests bytes at runtime.
  2. NULL check handles allocation failure.
  3. Loop fills and prints the array.
  4. free returns the block when finished.

Example 4 — Count digits with ctype.h

Scan a C string and count digit characters with 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;
}
ctype.h Tutorial

How It Works

  1. ctype.h adds character tests like isdigit.
  2. Cast to unsigned char before passing bytes (portable and safe).
  3. Loop walks each character of the string.
  4. Count increments when the character is a digit.

Example 5 — Timestamped log line

Combine stdio, string, 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;
}
time.h Tutorial

How It Works

  1. time.h supplies the clock and strftime.
  2. string.h measures the message length.
  3. stdio.h prints the formatted log line.
  4. Real programs mix headers the same way—one job per include.

C Library Headers Index

19 standard headers in this C tutorial track. Search by name or browse by category. Each row opens a full guide with syntax, examples, and FAQs.

Diagnostics & errors

2 headers

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

HeaderDescriptionTutorial
<assert.h>assert macro for debug checks.Try it
<errno.h>errno and error reporting.Try it

Types & limits

5 headers

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

HeaderDescriptionTutorial
<stddef.h>size_t, NULL, offsetof.Try it
<stdint.h>int32_t, uint8_t fixed widths.Try it
<stdbool.h>bool, true, false (C99).Try it
<limits.h>INT_MAX, CHAR_BIT limits.Try it
<float.h>FLT_MAX, DBL_EPSILON.Try it

Input/output & strings

2 headers

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

HeaderDescriptionTutorial
<stdio.h>printf, scanf, fopen, fgets.Try it
<string.h>strcpy, strlen, strcmp, memcpy.Try it

Memory & utilities

2 headers

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

HeaderDescriptionTutorial
<stdlib.h>malloc, free, atoi, rand, exit.Try it
<stdarg.h>va_list for variadic functions.Try it

Characters & locale

4 headers

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

HeaderDescriptionTutorial
<ctype.h>isalpha, isdigit, tolower.Try it
<locale.h>setlocale, formatting rules.Try it
<wchar.h>Wide strings, wprintf, mbstowcs.Try it
<wctype.h>iswalpha, towlower wide ctype.Try it

Math & time

2 headers

Floating-point math and calendar/CPU time.

HeaderDescriptionTutorial
<math.h>sqrt, sin, pow (link -lm).Try it
<time.h>time, strftime, difftime.Try it

Advanced control

2 headers

Non-local jumps and asynchronous signal handling.

HeaderDescriptionTutorial
<setjmp.h>setjmp, longjmp stack escape.Try it
<signal.h>signal handlers, SIGINT.Try it

Edge Cases & Pitfalls

Most library bugs in C tutorials are missing includes, missing -lm, or unsafe string/memory use.

inc

Implicit declaration warnings

Calling printf without #include <stdio.h> is undefined in modern C. Include the header.

-lm

undefined reference to sqrt

Including math.h is not enough on GCC/Clang—add -lm at the end of the link line.

buf

strcat / strcpy overflows

Destination buffers must be large enough. Prefer bounded copies when available, and track capacity.

NULL

Unchecked malloc / fopen

Always test for NULL before using the pointer or FILE*.

ctype

Passing char to isdigit

Cast to (unsigned char) (or use EOF) so values above 127 are safe.

hdr

Assuming transitive includes

If you use a name from string.h, include string.h—even if another header happened to include it on your machine.

Platform Support

The headers on this page are part of standard C. Linking details differ mainly for math.

ToolchainSupportNotes
GCC (Linux / MinGW)Fulllibc automatic; math usually needs -lm
Clang (macOS / Linux)FullSame pattern as GCC for math.h
MSVCFullMath often links automatically
Freestanding / embeddedVariesMay lack full hosted library; check vendor docs

Hosted C environments provide stdio, stdlib, and string without extra flags. Wide-character and locale headers are standard but less common in early tutorials—learn them after the core three.

Key Takeaways

  • Headers declare; the linked library provides the code.
  • Start with stdio.h, string.h, and stdlib.h.
  • math.h often needs -lm on GCC/Clang.

One line: The C standard library is #include + link—learn stdio, string, and stdlib first, then use the headers index for the rest.

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. Include only the headers each .c file uses directly.
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 (often with -lm), and errno.h when system calls fail.
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. Including stdio.h lets the compiler check printf; the linker pulls in printf's implementation.
Practice the examples on this page, then open stdio.h, string.h, and stdlib.h. Use the headers index below when you need a specific API. Related tracks: C Strings and C Math.

Start with stdio.h

Open the I/O header first, then continue through string and stdlib in the sidebar.

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.

8 people found this page helpful