From d7d101a2bd0ac4c417ac924f849179510f9e1d81 Mon Sep 17 00:00:00 2001 From: kelson8 Date: Mon, 4 Nov 2024 14:44:45 -0500 Subject: [PATCH] Add files to repo, update gitignore. --- .gitignore | 15 ++++++++++++ .vscode/settings.json | 5 ++++ CMakeLists.txt | 18 ++++++++++++++ hello.c | 38 +++++++++++++++++++++++++++++ hello.h | 1 + main.c | 49 +++++++++++++++++++++++++++++++++++++ math_functions.c | 51 +++++++++++++++++++++++++++++++++++++++ math_functions.h | 18 ++++++++++++++ struct_test.c | 56 +++++++++++++++++++++++++++++++++++++++++++ struct_test.h | 28 ++++++++++++++++++++++ 10 files changed, 279 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 CMakeLists.txt create mode 100644 hello.c create mode 100644 hello.h create mode 100644 main.c create mode 100644 math_functions.c create mode 100644 math_functions.h create mode 100644 struct_test.c create mode 100644 struct_test.h diff --git a/.gitignore b/.gitignore index cd531cf..7f71029 100644 --- a/.gitignore +++ b/.gitignore @@ -52,3 +52,18 @@ Module.symvers Mkfile.old dkms.conf +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +!.vscode/*.code-snippets + +# Local History for Visual Studio Code +.history/ + +# Built Visual Studio Code Extensions +*.vsix + +build/* +!build/build.xml \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..b460d47 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "math_functions.h": "c" + } +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..1d1ec04 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,18 @@ +cmake_minimum_required(VERSION 3.5.0) +project(Test_Project VERSION 0.1.0 LANGUAGES C) + +# Add more c files in here +add_executable(Test_Project +main.c +hello.c +math_functions.c +struct_test.c +) + +# https://discourse.cmake.org/t/how-to-properly-include-header-files/4762 + +# Add header files in here, not sure if I did this right. +target_include_directories(Test_Project +PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}" + +) diff --git a/hello.c b/hello.c new file mode 100644 index 0000000..ef53de4 --- /dev/null +++ b/hello.c @@ -0,0 +1,38 @@ +#include +// #include +#include +#include "math_functions.h" + +// Project running in VSCode, this can be developed on Windows, Linux and Mac +// https://stackoverflow.com/questions/73328916/how-to-set-cmake-build-configuration-in-vscode + +// Basic C program that I am playing around with +// https://www.geeksforgeeks.org/c-hello-world-program/ + +// https://www.w3schools.com/c/c_functions_parameters.php +// https://www.w3schools.com/c/c_functions.php + + +// This doesn't work like this for some reason +int uint16_test(){ + // No value assigned + uint16_t test = -2; + + // Assign a value here + test = -2; + + return test; + // printf(test); +} + +// https://cplusplus.com/reference/cstdio/printf/ +void printf_example(){ + printf ("Characters: %c %c \n", 'a', 65); + printf ("Decimals: %d %ld\n", 1977, 650000L); + printf ("Preceding with blanks: %10d \n", 1977); + printf ("Preceding with zeros: %010d \n", 1977); + printf ("Some different radices: %d %x %o %#x %#o \n", 100, 100, 100, 100, 100); + printf ("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416); + printf ("Width trick: %*d \n", 5, 10); + printf ("%s \n", "A string"); +} diff --git a/hello.h b/hello.h new file mode 100644 index 0000000..9117b5f --- /dev/null +++ b/hello.h @@ -0,0 +1 @@ +void printf_example(); \ No newline at end of file diff --git a/main.c b/main.c new file mode 100644 index 0000000..1a7c206 --- /dev/null +++ b/main.c @@ -0,0 +1,49 @@ +#include +// #include +#include + +// My code +#include "math_functions.h" +#include "hello.h" +#include "struct_test.h" + +// Test +// https://stackoverflow.com/questions/8666378/detect-windows-or-linux-in-c-c +#ifdef _WIN32 +#include +#endif + + +// TODO Move this into a different project. +#ifdef _WIN32 + +void guiTest(){ + +} + +#endif + +// TODO Look into making a gtk C program +// https://www.geeksforgeeks.org/how-to-create-gui-in-c-programming-using-gtk-toolkit/ + +int main() { + char welcomeMsg[] = "Welcome to KCNet"; + // printf(welcomeMsg); + + uint16_t test = -2; + // This doesn't work + // uint16_test(); + + //? 65534? Where is that number coming from + // https://stackoverflow.com/questions/29112878/how-do-i-printf-a-uint16-t + // printf("Number: %u", (unsigned int)test); + + // https://stackoverflow.com/questions/12120426/how-do-i-print-uint32-t-and-uint16-t-variables-value + // printf("Number: %" PRIu32, test); + + // mathFunctions(); + print_struct(); + + // printf_example(); + return 0; +} \ No newline at end of file diff --git a/math_functions.c b/math_functions.c new file mode 100644 index 0000000..c6ffc47 --- /dev/null +++ b/math_functions.c @@ -0,0 +1,51 @@ +#include +#include "math_functions.h" + + +// I forgot how to do this in C. +// https://www.geeksforgeeks.org/c-function-argument-return-values/ + +// Well I built a basic calculator in C. +int add(int num1, int num2){ + int sum; + sum = num1 + num2; + + return sum; +} + +int subtract(int num1, int num2) { + int sum; + sum = num1 - num2; + + return sum; +} + +int divide(int num1, int num2) { + int sum; + sum = num1 / num2; + + return sum; +} + +int multiply(int num1, int num2) { + int sum; + sum = num1 * num2; + + return sum; +} + +// Uses functions I have defined in math_functions.h +void mathFunctions(){ + int a = 3, b = 2; + + // int num1 = 100, num2 = 55; + int addSum = add(a, b); + int subtractSum = subtract(a, b); + int multiplySum = multiply(a, b); + int divideSum = divide(a, b); + + printf("The sum of %d + %d = %d\n", a, b, addSum); + printf("The sum of %d - %d = %d\n", a, b, subtractSum); + printf("The sum of %d * %d = %d\n", a, b, multiplySum); + printf("The sum of %d / %d = %d\n", a, b, divideSum); +} \ No newline at end of file diff --git a/math_functions.h b/math_functions.h new file mode 100644 index 0000000..e2ee830 --- /dev/null +++ b/math_functions.h @@ -0,0 +1,18 @@ +// 10-29-2024 @ 2:25AM + +// https://www.learncpp.com/cpp-tutorial/header-guards/ +// #ifndef MATH_FUNCTIONS_H +// #define MATH_FUNCTIONS_H +// #endif + +// Oops I forgot to include this in the header like in C++. +// It worked without it but gave errors, I didn't think that would work. +// https://stackoverflow.com/questions/28170800/compiler-gives-warning-c4013-getche-undefined-assuming-extern-returning-in +extern void mathFunctions(); + +// Oops also I had the full definitions in here when the function should've been in the C file. +// It's been a little while since I've messed with a C type language +int add(int num1, int num2); +int subtract(int num1, int num2); +int divide(int num1, int num2); +int multiply(int num1, int num2); \ No newline at end of file diff --git a/struct_test.c b/struct_test.c new file mode 100644 index 0000000..a2d6dca --- /dev/null +++ b/struct_test.c @@ -0,0 +1,56 @@ +#include +#include + +#include + +#include "struct_test.h" + +// https://www.w3schools.com/c/c_structs.php + + +void print_struct(){ + struct TestStruct s1; + + s1.name = "Kelson"; + s1.age = 23; + + // https://stackoverflow.com/questions/10162152/how-to-work-with-string-fields-in-a-c-struct + // TestStruct *u = malloc(sizeof(struct TestStruct)); + // + + // strcpy(s1.name, "Kelson"); + // strcpy(s1.age, 23); + + // Oh, I had to add strdup to this for it to print. + // 11-4-2024 @ 2:38PM + printf("Name: %s\n", strdup(s1.name)); + printf("Age: %d\n", s1.age); + +} + + +#ifdef _TEST +// Not sure how to use this. +patient *create_patient(int number, char *name, +char *addr, char*bd, char sex) { + + // Allocate memory for the pointers themselves and other elements + // in the struct. + patient *p = malloc(sizeof(patient)); + + p->number = number; + + // Must allocate memory for contents of pointers. Here, strdup() + // creates a new copy of name. Another option: + // p->name = malloc(strlen(name)+1); + // strcpy(p->name, name); + + p->name = strdup(name); + p->address = strdup(addr); + p->birthdate = strdup(bd); + p->gender = sex; + + return p; +} + +#endif //_TEST \ No newline at end of file diff --git a/struct_test.h b/struct_test.h new file mode 100644 index 0000000..5328f5a --- /dev/null +++ b/struct_test.h @@ -0,0 +1,28 @@ +// Move structs in here for them to be usable in other files. + +// This might work for what I'm trying to do. +// https://www.geeksforgeeks.org/how-to-initialize-char-array-in-struct-in-c/ + +struct TestStruct { + char *name; + int age; +}; + +// Not sure how to use this +#ifdef _TEST +// https://stackoverflow.com/questions/10162152/how-to-work-with-string-fields-in-a-c-struct +typedef struct { + int number; + char *name; + char *address; + char *birthdate; + char gender; +} patient; + +patient *create_patient(int number, char *name, +char *addr, char*bd, char sex); + +#endif //_TEST + +void print_struct(); +