C

Quickstart (compile + run)

bash
cat > main.c <<'EOF'
#include <stdio.h>
int main(void){ puts("hello"); }
EOF

cc -std=c11 -Wall -Wextra -O2 main.c -o app
./app

Project setup (Makefile)

makefile
CC=cc
CFLAGS=-std=c11 -Wall -Wextra -O2

all: app
app: main.c
	$(CC) $(CFLAGS) main.c -o app
clean:
	rm -f app

Hello + argc/argv

c
#include <stdio.h>

int main(int argc, char **argv) {
  (void)argc;
  (void)argv;
  printf("hello\n");
  return 0;
}

Struct + pointer

c
typedef struct {
  int x;
  int y;
} Point;

void move(Point *p, int dx, int dy) {
  p->x += dx;
  p->y += dy;
}

Variables + basic types

c
int i = 1;
double d = 3.14;
char c = 'A';
const char *s = "hi";

Arrays

c
int xs[3] = {1, 2, 3};
size_t n = sizeof(xs) / sizeof(xs[0]);

Iteration (for/while)

c
for (int i = 0; i < 3; i++) {
  // ...
}

int j = 0;
while (j < 3) {
  j++;
}

Functions (prototype + definition)

c
int add(int a, int b);

int add(int a, int b) {
  return a + b;
}

Pointers (address, dereference)

c
int x = 10;
int *p = &x;
*p = 11;