C

A.K.A. High-level assembler

For when you gotta go fast

Created by jyn with reveal.js

5 minute history

  1. C is the oldest programming language still widely used (except maybe COBOL)
  2. In 1972, Dennis Ritchie had to program on this machine:
  3. His compiler was fast and free so people used it
  4. Called high-level assembler because it is very close to how a CPU operates

What is C good for?

  • Going fast
  • Talking directly to hardware (memory-mapped I/O)
  • Making system calls and FFI calls

Don't worry if you don't know these words yet

Enough meta, what does C look like?

Threaded Web Server

(demo goes here)

Preprocessor

  • #include — C doesn't have imports, it just puts an entire file inside of your file
  • #define A 1 — for when you want to be sure things are inlined
  • #ifdef — conditional compilation

Header files

  • C used to use a single-pass compiler, so it has to be very stupid
  • The compiler only knows the type of a function if you declare it before you use it
  • Also, C doesn't have public/private, static is the poor man's private

Pointers!

Pointers hold the address in memory of a variable

picture credit: stackoverflow.com

What's the difference?

int f(int i) {
  i = 1;
}
int f(int *p) {
  *p = 1;
}

Arrays in C are pointers

  • Have to know the length ahead of time
  • Array index is offset from a pointer
  • This is why arrays are 0-indexed!
int main() {
  int a[] = {1, 2, 3};
  int i;
  // these are all equivalent and set i to 1
  i = a[0];
  i = *a;
  i = *(a + 0);
  i = *(0 + a);
  i = 0[a];
  // this will not compile
  i = a; // error: assignment makes integer from pointer
}

Strings in C are arrays

  • No classes!
  • Have to know the length of a string somehow
  • Denoted by null character '\0'
picture credit: dyclassroom.com

How do you return multiple things in C?

  • In Java, you return a class
  • In Python, you return a tuple
    (a, b)
  • In C, you have output parameters

Signals

(demo goes here)

picture credit: gerhardmueller.de. You can tell I was really having trouble finding pictures.