Skip to content

Instantly share code, notes, and snippets.

@goose121
Created January 3, 2018 18:31
Show Gist options
  • Select an option

  • Save goose121/ea2981078f3961f5d1c01a358ace357a to your computer and use it in GitHub Desktop.

Select an option

Save goose121/ea2981078f3961f5d1c01a358ace357a to your computer and use it in GitHub Desktop.
FizzBuzz (function pointers)
#include <stdio.h>
void fizz(int i) {
printf("fizz\n");
}
void buzz(int i) {
printf("buzz\n");
}
void fizzbuzz(int i) {
printf("fizzbuzz\n");
}
void p_int(int i) {
printf("%d\n", i);
}
void (*cases[])(int) = {&p_int, &fizz, &buzz, &fizzbuzz};
void main(void) {
int i;
for(i = 1; i <= 100; ++i)
cases[ ((i % 5 == 0) << 1) + (i % 3 == 0) ](i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment