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
c.stdlib.1— library overview.c.core.3— integer types.
Core Concept
<stddef.h>
Common definitions used across the library:
| Name | Meaning |
|---|---|
size_t | unsigned type for object sizes |
ptrdiff_t | signed type for pointer differences |
NULL | null pointer constant |
offsetof(type, member) | byte offset of a struct member |
max_align_t | type 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
- Passing a negative or out-of-range value to
isalpha(UB). - Using
intinstead ofsize_tfor sizes and indices. - Assuming
boolis a distinct type rather than_Bool. - Forgetting that
NULLis for pointers, not integers.
Undefined Behavior
- Passing a value outside
EOF/unsigned charrange to<ctype.h>functions.
VERIFIED
Portability
size_t/ptrdiff_twidths are implementation-defined.bool/true/falseare macros in C99–C17, keywords in C23.
Practical Usage
- Use
size_tfor sizes,ptrdiff_tfor pointer differences. - Use
<stdint.h>types for fixed-width needs. - Cast
chararguments tounsigned charbefore passing to<ctype.h>.
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.
Related Concepts
c.core.3— integer types.c.core.26— structs and offsetof.c.stdlib.10— locale and wide characters.
References
- ISO/IEC 9899:2018 §7.4 (ctype.h), §7.19 (stddef.h), §7.20 (stdint.h),
§7.18 (stdbool.h), §5.2.4.2 (limits).
Verification
<ctype.h>argument range requirement.VERIFIEDboolis_Boolvia stdbool.h.VERIFIEDoffsetofis standard.VERIFIED- No example was executed during generation unless noted. `Execution not
verified.`
Progress
- [ ] Read
- [ ] Understand
- [ ] Complete examples
- [ ] Complete exercises
- [ ] Complete deep challenge
Concept checkboxes
- [ ] ctype.h functions
- [ ] stdint.h types
- [ ] stddef.h (size_t, ptrdiff_t, NULL, offsetof)
- [ ] stdbool.h
- [ ] limits.h and float.h
Mastery levels
| Concept | Current level (0–8) | Target level |
|---|---|---|
| c.lib.ctype | 0 | 5 |
| c.lib.stdint | 0 | 5 |