Snippets

Mark Wright FizzBuzz

Updated by Mark Wright

File src/main.c Modified

  • Ignore whitespace
  • Hide word diff
 #include <stdio.h>
 
+#define DIV3(i) (!(i%3))
+#define DIV5(i) (!(i%5))
+
 int main() {
   int i;
   for (i = 1; i <= 100; ++i)
     if (i % 3 && i % 5)
       printf("%d\n", i);
     else
-      printf("%s\n", i % 3 ? "Buzz" : (i % 5 ? "Fizz" : "FizzBuzz"));
+      printf("%s\n", DIV3(i) ? (DIV5(i) ? "FizzBuzz" : "Fizz") : "Buzz");
   return 0;
 }
Updated by Mark Wright

File README.md Modified

  • Ignore whitespace
  • Hide word diff
 
 Compile with `./configure && make && sudo make install`.
 
-# Usage
+# Getting Started
 
-Run `fizzbuzz`, and it will solve the challange.
+```
+$ fizzbuzz
+```
 
 # License
 
Updated by Former user

File Makefile Modified

  • Ignore whitespace
  • Hide word diff
-all:
+all: src/main.c
 	gcc src/main.c -O2 -o fizzbuzz
+install: fizzbuzz
+	mv fizzbuzz /usr/local/bin/
+uninstall: /usr/local/bin/fizzbuzz
+	rm /usr/local/bin/fizzbuzz
+clean: fizzbuzz
+	rm fizzbuzz
Updated by Former user

File Makefile Added

  • Ignore whitespace
  • Hide word diff
+all:
+	gcc src/main.c -O2 -o fizzbuzz

File src/loop.c Deleted

  • Ignore whitespace
  • Hide word diff
-#include <stdio.h>
-#include <stdlib.h>
-
-#include "loop.h"
-#include "print.h"
-
-int loop_through_string(string out, void (*in)(char)) {
-  int i;
-  for (i = 0; ISNTENDOFSTRING(i); ++i) {
-    (*in)(*(out+i));
-  }
-  return 0;
-}
-
-void feed_characters(string out, void (*in)(char)) { // Wrapper around loop_through_string
-  int return_value;
-  return_value = loop_through_string(out, in);
-  if (return_value) {
-    print_error_string("Unable to loop through string");
-    exit(1);
-  }
-}

File src/loop.h Deleted

  • Ignore whitespace
  • Hide word diff
-#ifndef LOOP_H
-#define LOOP_H
-
-#ifdef NULL
-#undef NULL
-#define NULL 0
-#endif
-
-#define ISNTNULL(i) ((i)!=NULL)
-#define ISNTENDOFSTRING(i) ISNTNULL(i)
-
-int loop_through_string(string out, void (*in)(char));
-void feed_characters(string out, void (*in)(char));
-
-#endif

File src/main.c Added

  • Ignore whitespace
  • Hide word diff
+#include <stdio.h>
+
+int main() {
+  int i;
+  for (i = 1; i <= 100; ++i)
+    if (i % 3 && i % 5)
+      printf("%d\n", i);
+    else
+      printf("%s\n", i % 3 ? "Buzz" : (i % 5 ? "Fizz" : "FizzBuzz"));
+  return 0;
+}

File src/print.c Deleted

  • Ignore whitespace
  • Hide word diff
-#include <stdio.h>
-
-#include "print.h"
-#include "loop.h"
-
-int print_character(FILE *file, char c) {
-  
-}
-
-int print_character_stdout(char c) { // Wrapper around print_character
-  int return_value;
-  return_value = print_character(stdout, c);
-  return return_value;
-}
-
-int print_string(FILE *file, string s) { // Wrapper around print_character
-  feed_characters(s, print_character);
-}
-
-int print_string_stdout(string s) { // Wrapper around print_string
-  int return_value;
-  return_value = print_string(stdout, s);
-  return return_value;
-}
-
-int print_error_string(string s) { // Wrapper around print_string
-  int return_value;
-  return_value = print_string(stderr, s);
-  return return_value;
-}
-
-int printInteger(int x) {
-  //TODO: print int
-}
-
-void flush_stdout() {
-  print_character_stdout('\n');
-}

File src/print.h Deleted

  • Ignore whitespace
  • Hide word diff
-#ifndef PRINT_H
-#define PRINT_H
-
-typedef char* string;
-
-int print_character(FILE *file, char c);
-int print_character_stdout(char c);
-int print_string(FILE *file, string s);
-int print_string_stdout(string s);
-int print_error_string(string s);
-int printInteger(int x);
-void flush_stdout();
-
-#endif
Updated by Former user

File src/loop.c Modified

  • Ignore whitespace
  • Hide word diff
+#include <stdio.h>
 #include <stdlib.h>
 
 #include "loop.h"

File src/loop.h Modified

  • Ignore whitespace
  • Hide word diff
 #ifndef LOOP_H
 #define LOOP_H
 
+#ifdef NULL
+#undef NULL
+#define NULL 0
+#endif
+
 #define ISNTNULL(i) ((i)!=NULL)
 #define ISNTENDOFSTRING(i) ISNTNULL(i)
 

File src/print.h Modified

  • Ignore whitespace
  • Hide word diff
 #ifndef PRINT_H
 #define PRINT_H
 
-#define string char *
+typedef char* string;
 
 int print_character(FILE *file, char c);
 int print_character_stdout(char c);
  1. 1
  2. 2
  3. 3
  4. 4
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.