A Folder Full of Tiny Experiments

My projects folder is full of tiny programs with names like struct_padding, atomic_ordering and float_to_int_rounding. Each one exists to answer a single question. How does the compiler lay out this struct? What does integer division do with negative numbers? Is this atomic actually lock-free on this hardware?

It’s tempting to answer questions like these by searching, or by reasoning them out. But a twenty-line program gives you an answer that’s specific to your compiler, your flags and your machine, and it’s often faster than reading three forum threads that disagree with each other.

Keeping them useful

A few habits make the folder worth keeping:

  1. One question per project. Name the folder after the question, not the answer.
  2. Write the answer down. A short README with what I expected, what actually happened, and the compiler version.
  3. Make it run in one command. A single source file and a build line. If it needs setup, I won’t rerun it in two years.
  4. Never clean up. Old experiments cost nothing to keep, and they’re surprisingly often relevant again.

An example

C rounds integer division toward zero, and the remainder takes the sign of the dividend:

#include <stdio.h>

int main(void) {
    printf("%d %d\n", -7 / 2, -7 % 2);   // -3 -1
    printf("%d %d\n",  7 / -2, 7 % -2);  // -3  1
    return 0;
}

Python, by contrast, rounds down: -7 // 2 is -4. It’s the kind of difference that’s obvious once you’ve seen it and baffling when you haven’t — and now there’s a folder that shows it in one command.