Skip to content

Commit c22e067

Browse files
committed
c misc
1 parent 5d5f2ad commit c22e067

File tree

6 files changed

+223
-0
lines changed

6 files changed

+223
-0
lines changed

Diff for: c/misc/header-files.c

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <stdio.h>
2+
3+
// filename.h
4+
/*
5+
#ifndef FILENAME_H
6+
#define FILENAME_H
7+
8+
const int number = 10;
9+
10+
#endif
11+
*/
12+
13+
#include "filename.h"
14+
15+
int main() {
16+
printf("%d\n", number); // 10
17+
}

Diff for: c/misc/linked-list.c

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// linked list implementation in C
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
5+
// element is struct with a value and a pointer to the next element
6+
struct Element {
7+
int value;
8+
struct Element *next;
9+
};
10+
11+
// allocate memory for element
12+
struct Element *create_element(int value) {
13+
struct Element *element = (struct Element*) malloc(sizeof(struct Element));
14+
15+
element->value = value;
16+
element->next = NULL;
17+
18+
return element;
19+
}
20+
21+
// insert
22+
void ll_insert(struct Element **element, int value) {
23+
struct Element *new_element = create_element(value);
24+
// list is empty
25+
if (*element == NULL) { *element = new_element; }
26+
// list is not empty
27+
else {
28+
struct Element *current = *element;
29+
// iterate until last element
30+
while (current->next != NULL) {
31+
current = current->next;
32+
}
33+
// insert new element
34+
current->next = new_element;
35+
}
36+
}
37+
38+
// remove
39+
void ll_remove(struct Element **element, int value) {
40+
struct Element *current = *element;
41+
struct Element *previous = NULL;
42+
// iterate until last element
43+
while (current != NULL) {
44+
// value is found
45+
if (current->value == value) {
46+
// value is first element
47+
if (previous == NULL) {
48+
*element = current->next;
49+
}
50+
// value is not first element
51+
else {
52+
previous->next = current->next;
53+
}
54+
// free memory
55+
free(current);
56+
return;
57+
}
58+
// update pointers
59+
previous = current;
60+
current = current->next;
61+
}
62+
}
63+
64+
// print
65+
void print(struct Element *element) {
66+
struct Element *current = element;
67+
// iterate until last element
68+
while (current != NULL) {
69+
printf("%d\n", current->value);
70+
current = current->next;
71+
}
72+
}
73+
74+
int main() {
75+
struct Element *ll = NULL;
76+
77+
// insert
78+
ll_insert(&ll, 5);
79+
ll_insert(&ll, 10);
80+
ll_insert(&ll, 15);
81+
ll_insert(&ll, 20);
82+
ll_insert(&ll, 25);
83+
ll_insert(&ll, 30);
84+
print(ll); // 5 10 15 20 25 30
85+
86+
// remove
87+
ll_remove(&ll, 15);
88+
ll_remove(&ll, 30);
89+
print(ll); // 5 10 20 25
90+
}

Diff for: c/misc/macros.c

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <stdio.h>
2+
#include <stddef.h>
3+
4+
// stringify error messages
5+
#define error_message(error) \
6+
printf("error: " #error "\n")
7+
8+
// if not defined (used in header files to avoid multiple inclusion)
9+
#ifndef MESSAGE
10+
#define MESSAGE "This is the message if not already defined."
11+
#endif
12+
13+
// parameterized macros
14+
#define square(x) ((x) * (x))
15+
#define max(x, y) ((x) > (y) ? (x) : (y))
16+
17+
int main() {
18+
printf("%s\n", __FILE__); // macros.c
19+
printf("%s\n", __DATE__); // Aug 21 2019
20+
printf("%s\n", __TIME__); // 01:22:18
21+
printf("%d\n", __LINE__); // 12
22+
printf("%d\n", __STDC__); // 1
23+
24+
error_message("This is an error.");
25+
// error: "This is an error."
26+
27+
printf("%s\n", MESSAGE);
28+
// This is the message if not already defined.
29+
30+
printf("%d\n", square(2)); // 4
31+
printf("%d\n", max(4,5)); // 5
32+
}

Diff for: c/misc/memory.c

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
/*
6+
- allocate an array of n elements of size s
7+
void *calloc(int n, int s);
8+
9+
- release a block of memory at address
10+
void free(void *address);
11+
12+
- allocate an array of b bytes (uninitialised)
13+
void *malloc(int b);
14+
15+
- re-allocate memory at address (extending to new size s)
16+
void *realloc(void *address, int s);
17+
*/
18+
19+
int main() {
20+
char name[36];
21+
char *description; // pointer to char without size limit
22+
23+
// copy string of 35 bytes into buffer of 36 bytes
24+
strcpy(name, "Wolfeschlegelsteinhausenbergerdorff");
25+
26+
// allocate memory dynamically (can result in fragmented memory)
27+
// description = malloc(10 * sizeof(char));
28+
// strcpy(description, "A too long text to store in only 10 bytes...");
29+
// printf("%s\n", description);
30+
// malloc(): corrupted top size
31+
// Aborted
32+
33+
description = malloc(100 * sizeof(char));
34+
strcpy(description, "A too long text to store in only 10 bytes...");
35+
printf("%s\n", description);
36+
// A too long text to store in only 10 bytes...
37+
38+
// free memory
39+
free(description);
40+
}

Diff for: c/misc/pointers.c

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <stdio.h>
2+
#include <stddef.h>
3+
4+
int main() {
5+
int num = 5;
6+
int *ip = NULL; // null pointer
7+
8+
printf("%p\n", ip); // 0x0 (nil)
9+
10+
// point to address of num
11+
ip = &num;
12+
13+
printf("%p\n", &num); // 0x7ffdb78c2664
14+
printf("%p\n", ip); // 0x7ffdb78c2664
15+
16+
// dereference (get value at address)
17+
printf("%d\n", *ip); // 5
18+
}

Diff for: c/misc/valist.c

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <stdio.h>
2+
#include <stdarg.h>
3+
4+
// declare function (normally in header file)
5+
double average(int n, ...);
6+
// define function
7+
double average(int n, ...) {
8+
va_list valist; /* stdarg.h */
9+
// return value
10+
double sum = 0.0;
11+
// init valist for n arguments
12+
va_start(valist, n);
13+
// access arguments
14+
for (int i=0; i<n; i++) {
15+
sum += va_arg(valist, int);
16+
}
17+
// free memory
18+
va_end(valist);
19+
// success
20+
return sum / n;
21+
}
22+
23+
int main() {
24+
printf("average of {1,2,3,4,5}: %f\n", average(5, 1, 2, 3, 4, 5));
25+
// average of {1,2,3,4,5}: 3.000000
26+
}

0 commit comments

Comments
 (0)