summaryrefslogtreecommitdiff
path: root/c
diff options
context:
space:
mode:
Diffstat (limited to 'c')
-rw-r--r--c/LICENSE24
-rw-r--r--c/README.md17
-rw-r--r--c/guess.c40
-rw-r--r--c/hello_world.c6
4 files changed, 87 insertions, 0 deletions
diff --git a/c/LICENSE b/c/LICENSE
new file mode 100644
index 0000000..68a49da
--- /dev/null
+++ b/c/LICENSE
@@ -0,0 +1,24 @@
+This is free and unencumbered software released into the public domain.
+
+Anyone is free to copy, modify, publish, use, compile, sell, or
+distribute this software, either in source code form or as a compiled
+binary, for any purpose, commercial or non-commercial, and by any
+means.
+
+In jurisdictions that recognize copyright laws, the author or authors
+of this software dedicate any and all copyright interest in the
+software to the public domain. We make this dedication for the benefit
+of the public at large and to the detriment of our heirs and
+successors. We intend this dedication to be an overt act of
+relinquishment in perpetuity of all present and future rights to this
+software under copyright law.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+
+For more information, please refer to <http://unlicense.org/>
diff --git a/c/README.md b/c/README.md
new file mode 100644
index 0000000..d89410b
--- /dev/null
+++ b/c/README.md
@@ -0,0 +1,17 @@
+# Learn C
+
+This is my personal repository of files for learning the C programming language.
+
+## Building
+
+In order to build files on macOS, run the following command:
+
+```sh
+cc hello_world.c -o hello_world
+```
+
+Once compiled, they can be executed:
+
+```sh
+./hello_world
+```
diff --git a/c/guess.c b/c/guess.c
new file mode 100644
index 0000000..e9ab24b
--- /dev/null
+++ b/c/guess.c
@@ -0,0 +1,40 @@
+#include <time.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+// Create separate function to get a random number between 0 - 100
+int get_rand() {
+ srand(time(NULL));
+ int r = rand() % 100;
+ return r;
+}
+
+int main() {
+ // Call function to get a random number between 0 - 100
+ int random_number = get_rand();
+ printf("Random number: %d", random_number);
+
+ // Set a qualifier to check if the user has guessed correctly
+ int user_success = 0;
+
+ // Create a loop to allow the user to guess as many times as they want
+ while (user_success == 0) {
+ // Read user input
+ char line[256];
+ if(fgets(line, sizeof line, stdin) != NULL)
+ {
+ // Convert user input to an integer
+ int user_input = atoi(line);
+
+ // Do the main comparison to determine if the guess is correct
+ if (user_input == random_number) {
+ user_success = 1;
+ printf("Congratulations! You solved the puzzle!");
+ } else {
+ printf("Sorry, that's not correct! Please try again:\n");
+ }
+ }
+ }
+
+ return 0;
+}
diff --git a/c/hello_world.c b/c/hello_world.c
new file mode 100644
index 0000000..648e6b0
--- /dev/null
+++ b/c/hello_world.c
@@ -0,0 +1,6 @@
+#include <stdio.h>
+
+int main() {
+ printf("Hello world!");
+ return 0;
+}