Odd code behaviour, just FYA

Here's a fun one: https://godbolt.org/z/sjGaGP46s

#include <cstdio>

int main() {
    while (true)
        ; // nothing
}

// This function is never called ... or is it?
void launch_the_nukes() {
    std::puts("Launching the nukes ...");
}

Guess what it does when you execute the program?

Click to expand the answer

It prints "Launching the nukes ...", even though the launch_the_nukes function was never called!


In assembly, sure, but you cannot reason about (relatively) higher-level languages like C or C++ in the same way.
C++ code gets "executed" on the Abstract Machine emulated by your compiler and optimizer, not on your actual hardware.

2 Likes