More C code

master
Drew Bednar 3 years ago
parent a6d1d6ce77
commit 14ef6b3ba0

@ -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 <binary>` can show the same thing.
### Order matters in dependencies

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

@ -8,3 +8,4 @@ $(P): $(OBJECTS)
clean:
rm -f *.o $(P)
rm -

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

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

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

@ -1,5 +1,5 @@
CC=gcc
CFLAGS=-Wall -O1
CFLAGS=-Wall -O1 -Wpadded
PROGRAMS=pixel struct2
all: $(PROGRAMS)

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

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

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

@ -0,0 +1,36 @@
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>
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;
}

@ -0,0 +1,23 @@
#include <stdio.h>
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.
}
Loading…
Cancel
Save