From 3ec668ac415d2d92b28e658b17d8932d0cbe2b3b Mon Sep 17 00:00:00 2001 From: Christian Cleberg Date: Mon, 5 May 2025 18:09:27 -0500 Subject: initial commit --- c/LICENSE | 24 ++++++++++++++++++++++++ c/README.md | 17 +++++++++++++++++ c/guess.c | 40 ++++++++++++++++++++++++++++++++++++++++ c/hello_world.c | 6 ++++++ 4 files changed, 87 insertions(+) create mode 100644 c/LICENSE create mode 100644 c/README.md create mode 100644 c/guess.c create mode 100644 c/hello_world.c (limited to 'c') 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 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 +#include +#include + +// 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 + +int main() { + printf("Hello world!"); + return 0; +} -- cgit v1.2.3-70-g09d2