C Mastery / ctype.h, stdint.h, stddef.h, stdbool.h, limits.h, float.h
Part 3 — The Standard Library

ctype.h, stdint.h, stddef.h, stdbool.h, limits.h, float.h

This chapter covers the "support" headers: character classification (<ctype.h>), fixed-width integer types (<stdint.h>), common definitions (<stddef.h>), boolean macros (<stdbool.h>), and limits (<limits.h>, <float.h>).

Why This Matters

These headers provide the type definitions, constants, and macros that make C code portable and self-documenting. They are small but used everywhere.

Prerequisites

Core Concept

<stddef.h>

Common definitions used across the library:

NameMeaning
size_tunsigned type for object sizes
ptrdiff_tsigned type for pointer differences
NULLnull pointer constant
offsetof(type, member)byte offset of a struct member
max_align_ttype with the largest alignment (C11)

<stdint.h>

Fixed-width and related integer types (see c.core.3 for details).

<stdbool.h>

Defines:

#define bool  _Bool
#define true  1
#define false 0

(C23 makes bool, true, false keywords; in C99–C17 they come from this header.)

<limits.h> and <float.h>

Integer and floating-point limits (e.g., INT_MAX, DBL_EPSILON).

<ctype.h>

Character classification and conversion:

isalpha, isdigit, isalnum, isspace, isupper, islower, isprint,
ispunct, isxdigit, iscntrl, isgraph, toupper, tolower

These take an int that is either EOF or representable as unsigned char; passing another value is UB.

Examples

Character classification

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

int main(void)
{
    char c = 'A';
    printf("%d %d\n", isalpha((unsigned char)c), isdigit((unsigned char)c));
    printf("%c\n", tolower((unsigned char)c));
    return 0;
}

Expected output: 1 0 then a.

offsetof

#include <stddef.h>
#include <stdio.h>

struct S { char c; int i; };

int main(void)
{
    printf("%zu\n", offsetof(struct S, i));
    return 0;
}

Expected output: 4 on typical systems.

Common Mistakes

Undefined Behavior

VERIFIED

Portability

Practical Usage

Exercises

1. Write a function that counts letters, digits, and spaces using <ctype.h>. 2. Use offsetof to print the offsets of several struct members. 3. Demonstrate the unsigned char cast requirement for <ctype.h>.

Deep Challenge

Explain why isalpha(c) with a char c that is negative (signed char) is UB, and show the correct (unsigned char) cast. Then write a small UTF-8-aware letter classifier using unsigned char bytes.

References

§7.18 (stdbool.h), §5.2.4.2 (limits).

Verification

verified.`

Progress

Concept checkboxes

Mastery levels

ConceptCurrent level (0–8)Target level
c.lib.ctype05
c.lib.stdint05