]> Pileus Git - ~andy/sfvlug/blobdiff - c/src/jump.c
Add C notes.
[~andy/sfvlug] / c / src / jump.c
diff --git a/c/src/jump.c b/c/src/jump.c
new file mode 100644 (file)
index 0000000..7fab02c
--- /dev/null
@@ -0,0 +1,44 @@
+#include <stdio.h>
+
+void bad_hello(int count)
+{
+       int i = 0;
+hello:
+       printf("Hello, %d!\n", i);
+       i++;
+       if (i < count)
+               goto hello;
+}
+
+int ok_hello(int count)
+{
+       if (count == 0)
+               goto error_zero;
+       if (count < 0)
+               goto error_neg;
+
+       for (int i = 0; i < count; i++)
+               printf("Hello, %d!\n", i);
+
+       return 0;
+
+error_zero:
+       printf("Count is zero!\n");
+       return 1;
+
+error_neg:
+       printf("Count is negative!\n");
+       return 1;
+}
+
+int main()
+{
+       bad_hello(3);
+       printf("\n");
+
+       ok_hello(1);
+       ok_hello(0);
+       ok_hello(-1);
+
+       return 0;
+}