Skip to content

Instantly share code, notes, and snippets.

@whitglint
Forked from lancetw/fizzBuzz.c
Last active February 14, 2017 05:54
Show Gist options
  • Select an option

  • Save whitglint/71adc75e77a0672e0ccf85c5eb76dd46 to your computer and use it in GitHub Desktop.

Select an option

Save whitglint/71adc75e77a0672e0ccf85c5eb76dd46 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
static const size_t sizeToAlloc = sizeof("9223372036854775807");
char** fizzBuzz(int n, int* returnSize) {
char** ret = calloc(n, sizeof(char*));
*returnSize = n;
char* sp;
for (int i = 1; i <= n; ++i) {
sp = *(ret + (i - 1)) = calloc(1, sizeToAlloc);
(i % 5 && i % 3) ? sprintf(sp, "%d", i) : sprintf(sp, "%s%s", (i % 3) ? "" : "Fizz", (i % 5) ? "" : "Buzz");
}
return ret;
}
int main() {
int n = 15;
int returnSize;
char **res = fizzBuzz(n, &returnSize);
for (int i = 0; i < n; ++i) {
printf("%s\n", res[i]);
free(res[i]);
}
free(res);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment