C Standard Library: , & Fixed-Width Integers Masterclass
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.
| Function / Type | Purpose |
|---|---|
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;
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.
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.
| Type | Size | Range | Use Case |
|---|---|---|---|
int8_t | 8 bits | -128 to 127 | Byte values, sensor readings |
uint8_t | 8 bits | 0 to 255 | Raw bytes, pixel channel values |
int16_t | 16 bits | -32768 to 32767 | Audio samples, small integers |
uint16_t | 16 bits | 0 to 65535 | Port numbers, 16-bit indices |
int32_t | 32 bits | ยฑ2.1 billion | General integers, pixel ARGB |
uint32_t | 32 bits | 0 to ~4.3 billion | IP addresses, file offsets |
int64_t | 64 bits | ยฑ9.2 quintillion | Timestamps, large counters |
size_t | Platform word | 0 to SIZE_MAX | sizeof results, array lengths |
#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;
}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).