Adding a lot of C code

master
Drew Bednar 5 years ago
parent 576a223a29
commit 743f5d215d

1
.gitignore vendored

@ -32,3 +32,4 @@
# Debug files # Debug files
*.dSYM/ *.dSYM/
.idea/

@ -0,0 +1,7 @@
P=
OBJECTS=
CFLAGS = -g -Wall -O3
LDLIBS=
CC=gcc
$(P): $(OBJECTS)

@ -0,0 +1,20 @@
# Environment Variables
Ok this is kind of getting interesting.
## Building
This makefile is kind of interesting. To build and name our program we just need to:
```
make P="env"
```
Ok I don't really know the magic that is going on behind that one. Notice though that you are passing that variable after calling the program.
Technically, I think it's supposed to work like this:
```
make CFLAGS="-g -Wall" Set a makefile variable.
CFLAGS="-g -Wall" make Set an environment variable that only make and its children see.
```
But on my mac I am noticing only the set makefile variable method works.

Binary file not shown.

@ -0,0 +1,14 @@
#include <stdlib.h> //getenv, atoi
#include <stdio.h> //printf
int main(){
// pointer to a char
char *repstext=getenv("reps");
int reps = repstext ? atoi(repstext) : 10;
char *msg = getenv("msg");
if (!msg) msg = "Hello.";
for (int i=0; i< reps; i++)
printf("%s\n", msg);
}

@ -0,0 +1,7 @@
csv_writer:
gcc -o csv_writer csv_writer.c
clean:
rm -f csv_writer

Binary file not shown.

@ -0,0 +1,56 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FILE_EXTENSION ".csv"
int main(int argc, char const *argv[]){
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){
fprintf(stderr, "Failed to allocate memory for file name!");
exit(1);
}
/* Copy file name from executable's first arguments.*/
strcpy(FILE_NAME, argv[1]);
/*Returns a pointer to the location the substring if found*/
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;
exit(1);
}
char name[128], phone_number[13];
// puts(FILE_NAME);
FILE *_target_file = fopen(FILE_NAME, "a");
if (_target_file == NULL){
exit(1);
}
puts("Name: ");
scanf("%s", name);
puts("Number: ");
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);
fclose(_target_file);
/*clean up*/
free(FILE_NAME);
FILE_NAME = 0;
return 0;
}

@ -0,0 +1 @@
Margot,517-775-7040
1 Margot 517-775-7040

@ -0,0 +1,11 @@
.PHONY: clean
P=temp_count
OBJECTS= temp.o temp_count.o
CFLAGS = -g -Wall -O3
LDLIBS=''
CC=gcc
$(P): $(OBJECTS)
clean:
rm -f *.o $(P)

Binary file not shown.

@ -0,0 +1,7 @@
.PHONY: clean
hello:
gcc hello.c -o hello -g -Wall -O3
clean:
rm -f *.o hello

@ -0,0 +1,17 @@
CFLAGS=-Wall -O3
PROGRAMS=simple_pointer adv_pointer
all: $(PROGRAMS)
simple_pointer:
gcc -o simple_pointer $(CFLAGS) simple_pointer.c
adv_pointer:
gcc -o adv_pointer $(CFLAGS) adv_pointer.c
# NOT IMPLEMENTED
#array_pointer:
# gcc -o array_pointer -Wall -O1 array_pointer.c
clean:
rm -f $(PROGRAMS)

@ -0,0 +1,5 @@
#Pointers
Following this [C Pointers Tutorial].
[C Pointers Tutorial]: https://www.cprogramming.com/tutorial/c/lesson6.html

Binary file not shown.

@ -0,0 +1,30 @@
#include <stdio.h>
#include <stdlib.h>
int main(void){
int o = 1337;
printf("Let's learn to use malloc!\n");
int *p = malloc(sizeof(*p));
/* We need to make sure we got the memory block*/
if (p){
fprintf(stdout, "Ok we got a good value from malloc we can assign a value.\n");
}
else {
fprintf(stderr, "Well fuck this didn't work. Malloc failed");
return 1;
}
printf("Ok the address of o is on the stack at: %p\n", &o);
printf("*p is on the heap at: %p\n", p);
printf("Let's assign a value to p: \n");
scanf("%d", p);
printf("We got %d\n", *p);
printf("Freeing up the memory\nClosing program...\n");
free(p);
p = 0;
return 0;
}

Binary file not shown.

@ -0,0 +1,18 @@
#include <stdio.h>
int main(void){
int x;
int *p;
/* Read it, "assign the address of x to p" */
p = &x;
printf("This should be random: %d\n", *p);
printf("Ok the address of x is %p\n", &x);
printf("Check that p has the same value: %p\n\n", p);
printf("Ok let's put a value in the pointer. Give me an int: \n");
scanf("%d", p);
printf("The value you provided was %d\n", *p);
printf("Let's check x since p was a pointer to x's memory address: %d\n", x);
}

@ -0,0 +1,5 @@
all:
gcc -o eggs -Wall -O3 eggs.c
clean:
rm -f eggs

Binary file not shown.

@ -0,0 +1,16 @@
CC=gcc
CFLAGS=-Wall -O1
PROGRAMS=pixel struct2
all: $(PROGRAMS)
pixel:
$(CC) -o pixel $(CFLAGS) pixel.c
struct2:
$(CC) -o struct2 $(CFLAGS) struct2.c
clean:
rm -f $(PROGRAMS)
.PHONY=clean

Binary file not shown.

Binary file not shown.
Loading…
Cancel
Save