Clang

The Overlord Languge and Necromancer of Old CPUs
status: progress
progress

Advanced C

// 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)) {
  printf("[");
  for (int i = 0; i < ARRSIZE; i++) {
    int ith = xes[i];
    if (predicate(ith)) {printf("%i", ith); if(i+1!=ARRSIZE) printf(", ");}
  }
  printf("];\n");
}

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) {
      newarr[i] = arr[i];
    }

    for(int i=0;i<ARRSIZE;++i) {
      prevj = (newarr[(i+prevj)%ARRSIZE])%(ARRSIZE);
      swap(&newarr[i], &newarr[prevj]);
    }
}

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
  printf("= Adding\n");
  int (*func1)(int, int) = foo;
  int res1 = func1(xes[0], xes[1]);
  printf("%i\n", res1);

  // printif(xes, iseven);
  printf("= printif(..., even)\n");
  bool (*func2)(int) = iseven;
  printif(xes, func2);

  // shuffling
  printf("= Shuffling\n");
  int shuffled[ARRSIZE] = {};
  shuffle_(shuffled, xes);
  printif(xes, all);
  printif(shuffled, all);

  // quicksort
  printf("= Sorted\n");
  qsort(xes, ARRSIZE, sizeof(int), compareint);
  printif(xes, all);
}

This is a Playlist created by Charles Cabergs.

The C Programming Language 2Ed

Assignment is an expression with the value of the LHS. One consequence: a = b = c = 1 is equivalent to a = (b = (c = 1)).

PDF Book

Meta