cpuzzle

hello world in C without semicolons and without IF/WHILE/FOR statements


My friend says it's possible to write a C program that will print "hello world" without IF/WHILE/FOR and without semicolons. After minimal research I told her it was not possible. Is it possible?


Solution

  • I've been trying to find a "portable" way of stealing a semicolon from an include file. This works under Linux:

    int main(int ac, char **av)
    {
    #define typedef
    #define uint8_t a[printf("hello world\n")]
    #include <stdint.h>
    }
    

    This causes the one typedef unsigned char uint8_t to become my printf.

    Another trick that worked was to #define away every standard stdint type such that stdint.h reduces to a bunch of semicolons.

    Both of these fall flat on FreeBSD because it uses private intermediate types (like __uint8_t) which means that removing typedef fails in the quoted example and prevents me from successfully removing all non-semicolons in the other case.

    It seems like it should be possible to steal a semicolon cleanly from an include file. Can anyone improve on my attempt?