diff --git a/.gitignore b/.gitignore index 4433abd..8246e48 100644 --- a/.gitignore +++ b/.gitignore @@ -32,4 +32,5 @@ # Debug files *.dSYM/ -.idea/ \ No newline at end of file +.idea/ +.vscode/ \ No newline at end of file diff --git a/21st_century_c/first_compile/Makefile b/21st_century_c/first_compile/Makefile new file mode 100644 index 0000000..c6175cf --- /dev/null +++ b/21st_century_c/first_compile/Makefile @@ -0,0 +1,12 @@ +.PHONY: clean +P=erf +OBJECTS= erf.o +CFLAGS = -g -Wall -O3 +LDLIBS='' +CC=gcc + +$(P): $(OBJECTS) + + +clean: + rm -f *.o $(P) diff --git a/21st_century_c/first_compile/erf b/21st_century_c/first_compile/erf deleted file mode 100755 index 8b55a02..0000000 Binary files a/21st_century_c/first_compile/erf and /dev/null differ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..97e69fa --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +CLEANSUBDIRS ?= types structs static pointers hello headers functions file_io env_vars + +cleanall: + @for subdir in $(CLEANSUBDIRS); do \ + (cd $$subdir && $(MAKE) clean) || exit 1; \ + done \ No newline at end of file diff --git a/env_vars/Makefile b/env_vars/Makefile index 8130219..1c83e8e 100644 --- a/env_vars/Makefile +++ b/env_vars/Makefile @@ -1,7 +1,11 @@ -P= +P=env OBJECTS= CFLAGS = -g -Wall -O3 LDLIBS= CC=gcc $(P): $(OBJECTS) + +clean: + rm -f *.o $(P) + rm -rf env.dSYM/ \ No newline at end of file diff --git a/env_vars/env b/env_vars/env deleted file mode 100755 index 015c364..0000000 Binary files a/env_vars/env and /dev/null differ diff --git a/env_vars/env.c b/env_vars/env.c index ba94c51..ef0c511 100644 --- a/env_vars/env.c +++ b/env_vars/env.c @@ -1,14 +1,18 @@ #include //getenv, atoi -#include //printf +#include //printf -int main(){ +int main() +{ // pointer to a char - char *repstext=getenv("reps"); + char *repstext = getenv("reps"); + // atoi reads in the string and converts it to a integer value. + // This is the tenary operator 'condition ? value_if_true : value_if_false' int reps = repstext ? atoi(repstext) : 10; - + char *msg = getenv("msg"); - if (!msg) msg = "Hello."; - - for (int i=0; i< reps; i++) + if (!msg) + msg = "Hello."; + + for (int i = 0; i < reps; i++) printf("%s\n", msg); } diff --git a/file_io/Makefile b/file_io/Makefile index 7362e1a..d1ee83e 100644 --- a/file_io/Makefile +++ b/file_io/Makefile @@ -4,4 +4,4 @@ csv_writer: gcc -o csv_writer csv_writer.c clean: - rm -f csv_writer + rm -f csv_writer *.csv diff --git a/file_io/csv_writer b/file_io/csv_writer deleted file mode 100755 index b6e6961..0000000 Binary files a/file_io/csv_writer and /dev/null differ diff --git a/file_io/csv_writer.c b/file_io/csv_writer.c index 34378b4..dd8d4dc 100644 --- a/file_io/csv_writer.c +++ b/file_io/csv_writer.c @@ -4,19 +4,21 @@ #define FILE_EXTENSION ".csv" +int main(int argc, char const *argv[]) +{ -int main(int argc, char const *argv[]){ - - if (argc <= 1){ + if (argc <= 1) + { fprintf(stderr, "Failed to provide a csv file\n"); exit(1); } size_t file_len = strlen(argv[1]); - + /*char pointer of*/ - char * FILE_NAME = malloc(file_len + 1); - if (FILE_NAME == NULL){ + char *FILE_NAME = malloc(file_len + 1); + if (FILE_NAME == NULL) + { fprintf(stderr, "Failed to allocate memory for file name!"); exit(1); } @@ -25,7 +27,8 @@ int main(int argc, char const *argv[]){ strcpy(FILE_NAME, argv[1]); /*Returns a pointer to the location the substring if found*/ - if (strstr(FILE_NAME, FILE_EXTENSION) == NULL ){ + if (strstr(FILE_NAME, FILE_EXTENSION) == NULL) + { fprintf(stderr, "What the fuck are you doing? That %s isn't a csv file!\n", FILE_NAME); free(FILE_NAME); FILE_NAME = 0; @@ -35,9 +38,9 @@ int main(int argc, char const *argv[]){ char name[128], phone_number[13]; // puts(FILE_NAME); FILE *_target_file = fopen(FILE_NAME, "a"); - if (_target_file == NULL){ + if (_target_file == NULL) + { exit(1); - } puts("Name: "); @@ -46,7 +49,7 @@ int main(int argc, char const *argv[]){ scanf("%s", phone_number); printf("Appending %s and %s to %s", name, phone_number, FILE_NAME); - fprintf(_target_file,"%s,%s\n", name, phone_number); + fprintf(_target_file, "%s,%s\n", name, phone_number); fclose(_target_file); /*clean up*/ free(FILE_NAME); diff --git a/file_io/dirp.csv b/file_io/dirp.csv deleted file mode 100644 index 75cadf7..0000000 --- a/file_io/dirp.csv +++ /dev/null @@ -1 +0,0 @@ -Margot,517-775-7040 diff --git a/functions/Makefile b/functions/Makefile new file mode 100644 index 0000000..6c82572 --- /dev/null +++ b/functions/Makefile @@ -0,0 +1,12 @@ +.PHONY: clean +P=cube +OBJECTS= cube.o +CFLAGS = -g -Wall -O3 +LDLIBS='' +CC=gcc + +$(P): $(OBJECTS) + + +clean: + rm -f *.o $(P) diff --git a/headers/temp_count b/headers/temp_count deleted file mode 100755 index f6560b5..0000000 Binary files a/headers/temp_count and /dev/null differ diff --git a/pointers/Makefile b/pointers/Makefile index 1e3c385..c65b1e3 100644 --- a/pointers/Makefile +++ b/pointers/Makefile @@ -1,5 +1,5 @@ CFLAGS=-Wall -O3 -PROGRAMS=simple_pointer adv_pointer +PROGRAMS=simple_pointer adv_pointer char_pointer all: $(PROGRAMS) @@ -9,6 +9,9 @@ simple_pointer: adv_pointer: gcc -o adv_pointer $(CFLAGS) adv_pointer.c +char_pointer: + gcc -o char_pointer $(CFLAGS) char_pointer.c + # NOT IMPLEMENTED #array_pointer: # gcc -o array_pointer -Wall -O1 array_pointer.c diff --git a/pointers/adv_pointer b/pointers/adv_pointer deleted file mode 100755 index 60ace4b..0000000 Binary files a/pointers/adv_pointer and /dev/null differ diff --git a/pointers/char_pointer.c b/pointers/char_pointer.c new file mode 100644 index 0000000..cad4fd6 --- /dev/null +++ b/pointers/char_pointer.c @@ -0,0 +1,33 @@ +#include + +#define DOLLARS 0x24; + +// Example of function declaration. This is what the preprocessor does with the header files +void set_to_y(char *p); + +int main() +{ + char my_char = 'a'; + char *p_char; + + p_char = &my_char; + + printf("The value of *p_char is set to %c\n", *p_char); + set_to_y(p_char); + + printf("Lets check the value of my_char: %c\n", my_char); + printf("You, know what I want to just muck with your memory directly\n"); + + //derefence the location and just set the byte we want + *p_char = DOLLARS; + printf("So all your bits now should show my_char as $ DOLLARS. Lets see: %c\n", my_char); + + return 0; +} + +// This is function definition +void set_to_y(char *p) +{ + fprintf(stdout, "Setting the value at address %p from %c to y\n", &p, *p); + *p = 'y'; +} \ No newline at end of file diff --git a/pointers/simple_pointer b/pointers/simple_pointer deleted file mode 100755 index d09d3f3..0000000 Binary files a/pointers/simple_pointer and /dev/null differ diff --git a/static/eggs b/static/eggs deleted file mode 100755 index c04439b..0000000 Binary files a/static/eggs and /dev/null differ diff --git a/structs/pixel b/structs/pixel deleted file mode 100755 index f7341f1..0000000 Binary files a/structs/pixel and /dev/null differ diff --git a/structs/struct2 b/structs/struct2 deleted file mode 100755 index 453e56b..0000000 Binary files a/structs/struct2 and /dev/null differ diff --git a/types/Makefile b/types/Makefile new file mode 100644 index 0000000..f910a7c --- /dev/null +++ b/types/Makefile @@ -0,0 +1,12 @@ +.PHONY: clean +P=types +OBJECTS= types.o +CFLAGS = -g -Wall -O3 +LDLIBS='' +CC=gcc + +$(P): $(OBJECTS) + + +clean: + rm -f *.o $(P) diff --git a/types/types.c b/types/types.c index f6bf39e..77fb24f 100644 --- a/types/types.c +++ b/types/types.c @@ -3,8 +3,6 @@ //Type definition typedef unsigned char byte; - - int main(int argc, char const *argv[]) { int i = 123; @@ -14,11 +12,11 @@ int main(int argc, char const *argv[]) //variations unsigned int ui = 123u; - + //short and long provide different storage depending on the compiler and platform //Regular int usually maps to the natural size of a word on a particular machine // Ex: on a 32 bit machine int == 32 bits short == 16 bits and long == 64 bits ...or 32 - short int si = 123; + short int si = 123; long int li = 123; //short hand declaration of short and long @@ -30,7 +28,7 @@ int main(int argc, char const *argv[]) //we have to cast the result of sizeof because it returns a value whose size is large enough //to hold a pointer value, but the fundamental types will never be that big so we cast - printf("The value of i: %d (%d) bytes\n", i, (int) sizeof(int)); + printf("The value of i: %d (%d) bytes\n", i, (int)sizeof(int)); //this will tell us that int is 4 bytes and running in a 32 bit address space return 0;