C Standard Library: , & Fixed-Width Integers Masterclass

โšก C (C17 / C23 Standard) ๐ŸŸข Lesson 45 ๐Ÿ“‚ Phase 17: Standard Library Deep-Dive ๐Ÿ“… 2026 Comprehensive Master Edition
๐Ÿ“Œ Covered in this in-depth guide: clock_t & time_t ยท Measuring Execution Time ยท bool type ยท int8_t to int64_t ยท PRId32 Format Macros ยท Platform-Independent Integers

Welcome to Phase 17 (Chapter 45): C Standard Library โ€” <time.h>, <stdbool.h> & <stdint.h> Fixed-Width Integers Masterclass! These three headers solve real-world portability and clarity problems. Fixed-width integers prevent silent integer truncation across platforms. Boolean types clarify intent. Time functions let you benchmark and schedule operations.

1time.h โ€” Timestamps & Execution Benchmarking
Function / TypePurpose
time_t time(NULL)Current Unix timestamp (seconds since Jan 1 1970 UTC).
clock_t clock()CPU time consumed by program. Use for benchmarking code sections.
difftime(t2, t1)Difference between two time_t values in seconds (double).
localtime(&t)Convert time_t to broken-down local struct tm.
strftime(buf, n, fmt, &tm)Format struct tm to human-readable string (like date command).

Benchmarking Pattern with clock():

clock_t start = clock();

... code to benchmark ...

double ms = (double)(clock() - start) / CLOCKS_PER_SEC * 1000.0;

2stdbool.h โ€” The bool Type in C99+

Before C99, C had no dedicated boolean type. Developers used integers (0 = false, non-zero = true). <stdbool.h> adds bool, true, and false as proper named types.

stdbool.h defines:

โ€ข bool โ€” Expands to _Bool (C99 built-in type, 0 or 1 only)

โ€ข true โ€” Integer constant 1

โ€ข false โ€” Integer constant 0

In C23, bool/true/false are built-in keywords and stdbool.h is no longer needed.

3stdint.h โ€” Fixed-Width Integer Types

The size of int, long, and char varies across 16-bit, 32-bit, and 64-bit platforms. <stdint.h> provides types with guaranteed exact widths โ€” essential for file formats, protocols, embedded systems, and bit manipulation.

TypeSizeRangeUse Case
int8_t8 bits-128 to 127Byte values, sensor readings
uint8_t8 bits0 to 255Raw bytes, pixel channel values
int16_t16 bits-32768 to 32767Audio samples, small integers
uint16_t16 bits0 to 65535Port numbers, 16-bit indices
int32_t32 bitsยฑ2.1 billionGeneral integers, pixel ARGB
uint32_t32 bits0 to ~4.3 billionIP addresses, file offsets
int64_t64 bitsยฑ9.2 quintillionTimestamps, large counters
size_tPlatform word0 to SIZE_MAXsizeof results, array lengths
4Complete Benchmark + Fixed-Width Demo
C โ€” Timing a sort with fixed-width integersโ–ถ Try in Compiler
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <time.h>

#define N 100000

static int cmp_int(const void *a, const void *b) {
    return (*(int32_t*)a > *(int32_t*)b) - (*(int32_t*)a < *(int32_t*)b);
}

int main(void) {
    srand((unsigned)time(NULL));
    int32_t *arr = malloc(N * sizeof(int32_t));
    if (!arr) { perror("malloc"); return 1; }

    for (int i = 0; i < N; i++) arr[i] = rand();

    clock_t start = clock();
    qsort(arr, N, sizeof(int32_t), cmp_int);
    double ms = (double)(clock() - start) / CLOCKS_PER_SEC * 1000.0;

    /* Verify sorted */
    bool sorted = true;
    for (int i = 1; i < N; i++) {
        if (arr[i] < arr[i-1]) { sorted = false; break; }
    }

    printf("Sorted %d int32_t values: %s\n", N, sorted ? "CORRECT" : "BUG!");
    printf("qsort time: %.3f ms\n", ms);

    free(arr);
    return 0;
}
5Technical FAQs

Q1: What is CLOCKS_PER_SEC?

It is a macro representing the number of clock() ticks per second (typically 1,000,000 on Linux). Divide clock() difference by it to get seconds.

Q2: What is int_fast32_t vs int32_t?

int32_t is exactly 32 bits. int_fast32_t is the fastest native integer type that is at least 32 bits โ€” may be 64-bit on 64-bit CPUs for performance.

Q3: How do I print int64_t portably?

Use <inttypes.h> format macros: printf("%" PRId64 "\n", value);. The PRId64 expands to the correct format specifier for the platform.

Q4: Is clock() reliable for wall-clock time?

No โ€” clock() measures CPU time (sum of all threads). For real elapsed wall time use clock_gettime(CLOCK_MONOTONIC, &ts) from <time.h> on POSIX systems.

Q5: What is the maximum value of size_t?

SIZE_MAX macro (from <stdint.h>) holds the maximum value. On 64-bit systems it is 18,446,744,073,709,551,615 (2^64-1).