Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define DIVTHREE 1
  5. #define DIVFIVE (1<<2)
  6. #define DIVBOTH (DIVTHREE | DIVFIVE)
  7. #define DIVNEITHER 0
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11.         long int start, end, current;
  12.         char *endp;
  13.  
  14.         if (argc < 2) {
  15.                 fprintf(stderr, "usage: %s <start> <end>\n", argv[0]);
  16.                 exit(EXIT_FAILURE);
  17.         }
  18.  
  19.         start = strtol(argv[1], &endp, 10);
  20.         if (endp == argv[1]) {
  21.                 fprintf(stderr, "invalid start number.\n");
  22.                 exit(EXIT_FAILURE);
  23.         }
  24.  
  25.         end = strtol(argv[2], &endp, 10);
  26.         if (endp == argv[2]) {
  27.                 fprintf(stderr, "invalid end number.\n");
  28.                 exit(EXIT_FAILURE);
  29.         }
  30.  
  31.         if (end < start) {
  32.                 fprintf(stderr, "end number should not be less than start.\n");
  33.                 exit(EXIT_FAILURE);
  34.         }
  35.  
  36.         for (current = start; current <= end; current++) {
  37.                 switch ( (current % 3 ? 0 : DIVTHREE) |
  38.                          (current % 5 ? 0 : DIVFIVE) ) {
  39.                         case DIVTHREE:
  40.                                 printf("fizz\n");
  41.                                 break;
  42.                         case DIVFIVE:
  43.                                 printf("buzz\n");
  44.                                 break;
  45.                         case DIVBOTH:
  46.                                 printf("fizzbuzz\n");
  47.                                 break;
  48.                         case DIVNEITHER:
  49.                                 printf("%ld\n", current);
  50.                                 break;
  51.                         default:
  52.                                 fflush(stdout);
  53.                                 fprintf(stderr, "your CPU is broken.\n");
  54.                                 exit(EXIT_FAILURE);
  55.                 }
  56.         }
  57.  
  58.         exit(EXIT_SUCCESS);
  59. }