Add files to repo, update gitignore.

This commit is contained in:
kelson8 2024-11-04 14:44:45 -05:00
parent 2baaaf206f
commit d7d101a2bd
Signed by: kelson8
GPG Key ID: 3D738D9DD7239E13
10 changed files with 279 additions and 0 deletions

15
.gitignore vendored
View File

@ -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

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"files.associations": {
"math_functions.h": "c"
}
}

18
CMakeLists.txt Normal file
View File

@ -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}"
)

38
hello.c Normal file
View File

@ -0,0 +1,38 @@
#include <stdio.h>
// #include <stdint.h>
#include <inttypes.h>
#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");
}

1
hello.h Normal file
View File

@ -0,0 +1 @@
void printf_example();

49
main.c Normal file
View File

@ -0,0 +1,49 @@
#include <stdio.h>
// #include <stdint.h>
#include <inttypes.h>
// 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 <Windows.h>
#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;
}

51
math_functions.c Normal file
View File

@ -0,0 +1,51 @@
#include <stdio.h>
#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);
}

18
math_functions.h Normal file
View File

@ -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);

56
struct_test.c Normal file
View File

@ -0,0 +1,56 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#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

28
struct_test.h Normal file
View File

@ -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();