From 14ef6b3ba017197abe7bd51cd28c730c5d4c899f Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Sun, 1 May 2022 16:59:05 -0400 Subject: [PATCH] More C code --- 21st_century_c/first_compile/README.md | 2 ++ Makefile | 2 +- arrays/Makefile | 1 + functions/Makefile | 5 +++- functions/call_back.c | 22 ++++++++++++++++ hello/hello.c | 10 +++++++ structs/Makefile | 2 +- structs/pixel.c | 13 ++++++---- structs/struct2.c | 32 +++++++++++------------ types/Makefile | 8 +++++- types/all_types.c | 36 ++++++++++++++++++++++++++ types/swap.c | 23 ++++++++++++++++ 12 files changed, 130 insertions(+), 26 deletions(-) create mode 100644 functions/call_back.c create mode 100644 types/all_types.c create mode 100644 types/swap.c diff --git a/21st_century_c/first_compile/README.md b/21st_century_c/first_compile/README.md index 02dd518..6c7005f 100644 --- a/21st_century_c/first_compile/README.md +++ b/21st_century_c/first_compile/README.md @@ -30,6 +30,8 @@ gcc -I/usr/local/include use_useful.c -o use_useful -L/usr/local/lib -luseful - `-L` Adds the library (object code) to the Linker's search path. - `-l` Says to link the `useful` library +Note: I guess mac doesn't have `ldd` it looks like `otool -L ` can show the same thing. + ### Order matters in dependencies diff --git a/Makefile b/Makefile index 9784453..fc9c0cd 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CLEANSUBDIRS ?= types structs static pointers hello headers functions file_io env_vars linked_list +CLEANSUBDIRS ?= types structs static pointers hello headers functions file_io env_vars linked_list arrays cleanall: @for subdir in $(CLEANSUBDIRS); do \ diff --git a/arrays/Makefile b/arrays/Makefile index 1fb9916..db9b32f 100644 --- a/arrays/Makefile +++ b/arrays/Makefile @@ -8,3 +8,4 @@ $(P): $(OBJECTS) clean: rm -f *.o $(P) + rm - diff --git a/functions/Makefile b/functions/Makefile index 6c82572..99a60fb 100644 --- a/functions/Makefile +++ b/functions/Makefile @@ -8,5 +8,8 @@ CC=gcc $(P): $(OBJECTS) +call_back: + $(CC) -o call_back $(CFLAGS) call_back.c + clean: - rm -f *.o $(P) + rm -f *.o $(P) call_back diff --git a/functions/call_back.c b/functions/call_back.c new file mode 100644 index 0000000..c877d04 --- /dev/null +++ b/functions/call_back.c @@ -0,0 +1,22 @@ +#include + +void void_callback(void (*p_func)()) +{ + printf("Wrapped function start\n"); + (*p_func)(); //callback + printf("There I called your shitty function\n"); +}; + +void say_dirp() +{ + printf("DIRP!!!\n"); +} + +int main(void) +{ + void (*ptr)() = &say_dirp; // Ok so this is a weird way to declare pointer to a function but I guess that's what c has. + + void_callback(ptr); + + return 0; +} \ No newline at end of file diff --git a/hello/hello.c b/hello/hello.c index 7ca092d..a3dccce 100644 --- a/hello/hello.c +++ b/hello/hello.c @@ -3,8 +3,18 @@ int main() { + u_int8_t my_ubit = 255; + int8_t MY_BIT = 0x03; char name[10] = "Laura"; printf("Hello %s!\n", name); + + printf("The value of my_ubit: %d\n", my_ubit); + printf("The value of MY_BIT: %d\n", MY_BIT); + printf("This occuppies: %d bytes\n", (int)(sizeof(my_ubit))); + printf("The value of my_ubit: %d\n", my_ubit << 1); + my_ubit = my_ubit >> 1; + printf("The value of shifted my_ubit: %d\n", my_ubit); + return 0; } \ No newline at end of file diff --git a/structs/Makefile b/structs/Makefile index f1cb8f8..93ea8bf 100644 --- a/structs/Makefile +++ b/structs/Makefile @@ -1,5 +1,5 @@ CC=gcc -CFLAGS=-Wall -O1 +CFLAGS=-Wall -O1 -Wpadded PROGRAMS=pixel struct2 all: $(PROGRAMS) diff --git a/structs/pixel.c b/structs/pixel.c index 3160b99..cdd6b00 100644 --- a/structs/pixel.c +++ b/structs/pixel.c @@ -26,14 +26,14 @@ struct Pixel typedef struct Pixel Pixel; -void print_pixel(Pixel p){ +void print_pixel(Pixel p) +{ puts("Pixel Location"); printf("X:%2f, Y:%2f\n", p.X, p.Y); puts("Pixel color"); printf("rgb(%d, %d, %d)\n", p.color.Red, p.color.Green, p.color.Blue); } - int main(int argc, char const *argv[]) { //In C the compiler places structs in a different namespace @@ -43,15 +43,18 @@ int main(int argc, char const *argv[]) //struct Color c; // but we can instead introduce the structs via typedefs and leave off the struct keyword //Also we can initialize structs with the following - Color c = { 255, 128, 0 }; - Pixel p = { 10.0f, 20.0f, c}; + Color c = {255, 128, 0}; + Pixel p = {10.0f, 20.0f, c}; // Color c = { 255, 128 }; //Trailing members are zero initialized + printf("Just so you know you're 3 byte struct for Color will be padded. Padded size in bytes: %d\n", (int)sizeof(Color)); + printf("Size of Pixel: %d\n", (int)sizeof(Pixel)); + //We can access members of structs via dot notation like so float x = p.X; //We can use assignment to update a member directly c.Blue = 255; - Pixel o = { 12.0f, 22.0f, c}; + Pixel o = {12.0f, 22.0f, c}; print_pixel(p); print_pixel(p); diff --git a/structs/struct2.c b/structs/struct2.c index 3795f80..e3b18f5 100644 --- a/structs/struct2.c +++ b/structs/struct2.c @@ -5,38 +5,36 @@ // This means that the address in memory for first must be less than second and second less than third //2. Processors either require or prefer that types are alligned in memory such that they begin on an -//address boundry that is some multiple of it's size so if second was placed after first it would be -//misaligned +//address boundry that is some multiple of it's size so if second was placed after first it would be +//misaligned -//So what we are seeing here is that the second member is padded so that it is ensured to falls on a +//So what we are seeing here is that the second member is padded so that it is ensured to falls on a //on a 4 byte boundry relative to the start of the structure -//typedef struct -//{ -// -// short first; //xx__ -// int second; //xxxx -// short third; //xx__ //this end is padded to ensure that if multiple structures were in an array that they would also be aligned -//} Layout; +// typedef struct +// { +// short first; //xx__ +// int second; //xxxx +// short third; //xx__ //this end is padded to ensure that if multiple structures were in an array that they would also be aligned +// } Layout; //reordering can help align memory and reduce memory usage -// This results in an 8 byte struct +// // This results in an 8 byte struct typedef struct -{ +{ short first; //xx short third; //xx - int second; //xxxx + int second; //xxxx } Layout; - int main() { - printf("short: (%d) bytes\n", (int) sizeof(short)); + printf("short: (%d) bytes\n", (int)sizeof(short)); - printf("int: (%d) bytes\n", (int) sizeof(int)); + printf("int: (%d) bytes\n", (int)sizeof(int)); //What is interesting is that the struct as a whole occupies 12 bytes not 8 like we thought - printf("struct: (%d) bytes\n", (int) sizeof(Layout)); + printf("struct: (%d) bytes\n", (int)sizeof(Layout)); return 0; } \ No newline at end of file diff --git a/types/Makefile b/types/Makefile index 675c695..03831a0 100644 --- a/types/Makefile +++ b/types/Makefile @@ -7,7 +7,13 @@ CC=gcc $(P): $(OBJECTS) +swap: + $(CC) -o swap $(CFLAGS) swap.c + +all_types: + $(CC) -o all_types $(CFLAGS) all_types.c + clean: - rm -f *.o $(P) + rm -f *.o $(P) swap all_types diff --git a/types/all_types.c b/types/all_types.c new file mode 100644 index 0000000..4ea5580 --- /dev/null +++ b/types/all_types.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + + printf("CHAR_BIT : %d\n", CHAR_BIT); + printf("CHAR_MAX : %d\n", CHAR_MAX); + printf("CHAR_MIN : %d\n", CHAR_MIN); + printf("INT_MAX : %d\n", INT_MAX); + printf("INT_MIN : %d\n", INT_MIN); + printf("LONG_MAX : %ld\n", (long)LONG_MAX); + printf("LONG_MIN : %ld\n", (long)LONG_MIN); + printf("SCHAR_MAX : %d\n", SCHAR_MAX); + printf("SCHAR_MIN : %d\n", SCHAR_MIN); + printf("SHRT_MAX : %d\n", SHRT_MAX); + printf("SHRT_MIN : %d\n", SHRT_MIN); + printf("UCHAR_MAX : %d\n", UCHAR_MAX); + printf("UINT_MAX : %u\n", (unsigned int)UINT_MAX); + printf("ULONG_MAX : %lu\n", (unsigned long)ULONG_MAX); + printf("USHRT_MAX : %d\n", (unsigned short)USHRT_MAX); + + printf("\nStorage size for float : %d \n", sizeof(float)); + printf("FLT_MAX : %g\n", (float)FLT_MAX); + printf("FLT_MIN : %g\n", (float)FLT_MIN); + printf("-FLT_MAX : %g\n", (float)-FLT_MAX); + printf("-FLT_MIN : %g\n", (float)-FLT_MIN); + printf("DBL_MAX : %g\n", (double)DBL_MAX); + printf("DBL_MIN : %g\n", (double)DBL_MIN); + printf("-DBL_MAX : %g\n", (double)-DBL_MAX); + printf("Precision value: %d\n", FLT_DIG); + + return 0; +} \ No newline at end of file diff --git a/types/swap.c b/types/swap.c new file mode 100644 index 0000000..2f07bd9 --- /dev/null +++ b/types/swap.c @@ -0,0 +1,23 @@ +#include + +void swap(int *, int *); // defined + +int main(void) +{ + int a = 21; + int b = 17; + int *p_a = &a; //address of operator. Generates a pointer to the memory location og the object. + int *p_b = &b; // Assigning the pointer &b to the *p_b int pointer variable declared in line + + swap(p_a, p_b); //could have done swap(&a, &b); simulates pass-by-reference + + printf("main: a = %d, b = %d\n", a, b); + return 0; +} + +void swap(int *a, int *b) +{ + int t = *a; // dereference the pointer to save it's object's value to the t variable + *a = *b; // *a and *b are both derefencing the int objects at those memory locations. This overwrites the object at location a with b's value. + *b = t; // dereference the b pointer and overwrite it's object's value with the value of t. +} \ No newline at end of file