Code golfing FizzBuzz in 88 bytes of C
A small curio I found buried in my programming cellar that I thought would make for a fun
post. For those who don't know about it, here's a definition of the task at hand, now behold the complete C program that solves it (build with
gcc/clang -std=c89 fizz.c):
main(i){while(printf(i%3*i%5?"%d ":"%2$s%3$s ",i,i%3?"":"Fizz",i%5?"":"Buzz"),i++<100);}
Let me explain the various little tricks used here:
- Unlocking "sloppy mode" via ANSI C (aka C89) lets me compile without any
#includes and without having to type neithermain's return nor its parameter (the traditionally namedargc), which then default toint. - The use of
argcas loop variableisince it's conveniently initialized to 1 in this case (program launched as./a.out). - All the
printfcalls can be fused into one thanks to thePOSIXextension%n$allowing to specify which argument is to be fed to a format directive instead of "the next one". - Then finally, a simple use of the oftentimes forgotten sequence operator
,(comma) to gain 4 chars on the equivalentdo ... while.
So there you go! If you have a smaller version compiling in the same conditions (changing the
standard or adding -D_DEFAULT_SOURCE is allowed), please send it to me and I'll either link your URL or add your
solution to this page!