Skip to content

Instantly share code, notes, and snippets.

@17twenty
Last active February 11, 2021 14:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 17twenty/3019174 to your computer and use it in GitHub Desktop.
Save 17twenty/3019174 to your computer and use it in GitHub Desktop.
Shortest FizzBuzz in C. Implicit declaration of printf but libc is linked against any way so nyeh.
main(i){for(;i++<100;)printf(i%3?i%5?"%d\n":"Buzz\n":i%5?"Fizz\n":"FizzBuzz\n",i);}
@dotwaffle
Copy link

This should be shorter...

main(){for(int i=1;i<101;++i){printf((printf("%s%s",(i%3==0)?"Fizz":"",(i%5==0)?"Buzz":""))?"\n":"%d\n",i);}}

@17twenty
Copy link
Author

17twenty commented Jul 4, 2012

[nick@slimtop Development]$ gcc -std=c99 fizz_waffle.c -o fizz
fizz_waffle.c:1:1: warning: return type defaults to ‘int’ [enabled by default]
fizz_waffle.c: In function ‘main’:
fizz_waffle.c:1:1: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
fizz_waffle.c:1:31: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]

That's a lot of warnings but yeah, it's good!. I was going to use the for-loop initial declaration but for some reason didn't want to have to stipulate c99. Nothing like losing to hubris :D

@17twenty
Copy link
Author

17twenty commented Jul 7, 2012

Even shorter now.

@davestevens
Copy link

shaved 3 characters off it:
main(){for(int i=0;++i<101;)printf(printf("%s%s",i%3?"":"Fizz",i%5?"":"Buzz")?"\n":"%d\n",i);}

@17twenty
Copy link
Author

Cheers Dave, I've revised the gist!

@iamscottmoyers
Copy link

Shaved 5 characters off it:
main(){for(int i=0;++i<101;)printf(i%3?i%5?"%d\n":"Buzz\n":i%5?"Fizz\n":"FizzBuzz\n",i);}

@GavinHigham
Copy link

Shaved 10 characters off it:
main(i){for(i--;i++<100;)printf("%i%s%s\n",i,i%3?"\r":"\rfizz",i%5?"":"buzz");}

@GavinHigham
Copy link

Shaved another 4 off that:
main(i){for(;i<101;puts(i++%5?"":"buzz"))printf("%i\r%s",i,i%3?"":"fizz");}

@17twenty
Copy link
Author

oh that is nice Gavin!

Copy link

ghost commented Feb 11, 2021

Shaved another 4 off that:
main(i){for(;i<101;puts(i++%5?"":"buzz"))printf("%i\r%s",i,i%3?"":"fizz");}

-1 bytes by changing printf()
main(i){for(;i<101;puts(i++%5?"":"Buzz"))printf(i%3?i%5?"%d":0:"Fizz",i);}

removing the stop condition brings it down to 69 bytes and is still technically fizzbuzz
main(i){for(;;puts(i++%5?"":"Buzz"))printf(i%3?i%5?"%d":0:"Fizz",i);}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment