Skip to content

Instantly share code, notes, and snippets.

@travisperson
Created October 5, 2012 23:03
Show Gist options
  • Save travisperson/3842969 to your computer and use it in GitHub Desktop.
Save travisperson/3842969 to your computer and use it in GitHub Desktop.
Preprocessor fizzbuzz
// Requires -std=c99
#include <stdio.h>
#define divisible %
#define by
#define to ; i <=
#define when(mod, msg) \
if((i mod) == 0) \
{ \
printf(#msg); \
*replaced = 1; \
} \
#define fizzbuzz(range) \
void rules(int, short *); \
int main(void) { \
for(int i = range; ++i) \
{ \
short replaced = 0; \
rules(i, &replaced); \
if(!replaced) \
printf("%d", i); \
printf("\n"); \
} \
} \
void rules(int i, short *replaced) \
{
#define end }
fizzbuzz(1 to 100)
when(divisible by 3, Fizz)
when(divisible by 5, Buzz)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment