Clang
The Overlord Languge and Necromancer of Old CPUs
status: progress
progress
Advanced C
Part 1
// function pointers in clang
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
int foo(int num1, int num2) { return num1 + num2; }
bool iseven(int numer) {
int mod2 = (numer % 2);
int isdivisiblebytwo = mod2 == 0;
return isdivisiblebytwo;
}
bool all(int _) {
return true;
}
#define ARRSIZE 10
void printif(const int xes[ARRSIZE], bool (*predicate)(int)) {
("[");
printffor (int i = 0; i < ARRSIZE; i++) {
int ith = xes[i];
if (predicate(ith)) {printf("%i", ith); if(i+1!=ARRSIZE) printf(", ");}
}
("];\n");
printf}
void swap(int* foo, int* bar) {
int temp = *foo;
*foo = *bar;
*bar = temp;
}
void shuffle_(int newarr[ARRSIZE], const int arr[ARRSIZE]) {
// int newarr[ARRSIZE] = {};
int prevj = 1;
for(int i=0;i<ARRSIZE;++i) {
[i] = arr[i];
newarr}
for(int i=0;i<ARRSIZE;++i) {
= (newarr[(i+prevj)%ARRSIZE])%(ARRSIZE);
prevj (&newarr[i], &newarr[prevj]);
swap}
}
void map(int arr[ARRSIZE], int (*func)(int)) {
for(int i = 0; i < ARRSIZE; ++i) {arr[i] = func(i);}
}
int compareint(const void *int1, const void *int2) {
int diff = *((int*)int1)-*((int*)int2);
return diff;
}
int main(void) {
int xes[ARRSIZE] = {1,2,3,4,5,6,7,8,9,10};
// Adding
("= Adding\n");
printfint (*func1)(int, int) = foo;
int res1 = func1(xes[0], xes[1]);
("%i\n", res1);
printf
// printif(xes, iseven);
("= printif(..., even)\n");
printfbool (*func2)(int) = iseven;
(xes, func2);
printif
// shuffling
("= Shuffling\n");
printfint shuffled[ARRSIZE] = {};
(shuffled, xes);
shuffle_(xes, all);
printif(shuffled, all);
printif
// quicksort
("= Sorted\n");
printf(xes, ARRSIZE, sizeof(int), compareint);
qsort(xes, all);
printif}
Part 2
Meta
This is a Playlist created by Charles Cabergs.
The C Programming Language 2Ed
Note
Assignment is an expression with the value of the LHS. One consequence: a = b = c = 1
is equivalent to a = (b = (c = 1))
.
Chapter 1
Meta