XXIIVV

C is the native language of Unix. It has come to dominate systems programming in the computer industry.

Work on the first official C standard began in 1983. The major functional additions to the language were settled by the end of 1986, at which point it became common for programmers to distinguish between "K&R C" and ANSI C.

People use C because it feels faster. If you build a catapult that can hurl a bathtub with someone inside from London to New York, it will feel very fast both on take-off and landing, and probably during the ride, too, while a comfortable seat in business class on a transatlantic airliner would probably take less time but you would not feel the speed nearly as much. ~

Typical C89 Programmer

One good reason to learn C, even if your programming needs are satisfied by a higher-level language, is that it can help you learn to think at hardware-architecture level. For notes specific to the Plan9's C compiler, see Plan9 C.

Compile

To convert source to an executable binary one uses a compiler. My compiler of choice is tcc, but more generally gcc is what most toolchains will use on Linux.

cc -Wall -lm -o main main.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int count = 10;

int
add_together(int x, int y)
{
	int result = x + y;
	return result;
}

typedef struct {
	int x;
	int y;
	int z;
} point;

void
print_point(point point)
{
	printf("the point is: (%d,%d,%d)\n",point.x,point.y,point.z);
}

int
main(int argc, char** argv)
{
	point p;
	p.x = 2;
	p.y = 3;
	p.z = 4;

	float length = sqrt(p.x * p.x + p.y * p.y);

	printf("float: %.6f\n", length);
	printf("int: %d\n", p.z);

	print_point(p);

	return 0;
}

Include

Generally, projects will include .h files which in turn will include their own .c files. The following form is used for system header files. It searches for a file named file in a standard list of system directories.

#include <file>

The following form is used for header files of your own program. It searches for a file named folder/file.h in the directory containing the current file.

#include "folder/file.h"

IO

One way to get input into a program or to display output from a program is to use standard input and standard output, respectively. The following two programs can be used with the unix pipe ./o | ./i

o.c

#include <stdio.h>

int
main()
{
	printf("(output hello)");
	return 0;
}

i.c

#include <stdio.h>

int
main()
{
	char line[256];
	if(fgets(line, 256, stdin) != NULL) {
		printf("(input: %s)\n", line);
	}
	return 0;
}

Threads

Threads are a way that a program can spawn concurrent operations that can then be delegated by the operating system to multiple processing cores.

cc threads.c -o threads && ./threads
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define NTHREADS 5

void
*myFun(void *x)
{
	int tid;
	tid = *((int *) x);
	printf("Hi from thread %d!\n", tid);
	return NULL;
}

int
main(int argc, char *argv[])
{
	pthread_t threads[NTHREADS];
	int thread_args[NTHREADS];
	int rc, i;

	/* spawn the threads */
	for (i=0; i<NTHREADS; ++i)
	{
		thread_args[i] = i;
		printf("spawning thread %d\n", i);
		rc = pthread_create(&threads[i], NULL, myFun, (void *) &thread_args[i]);
	}

	/* wait for threads to finish */
	for (i=0; i<NTHREADS; ++i) {
		rc = pthread_join(threads[i], NULL);
	}

	return 1;
}

SDL

cc demo.c -I/usr/local/include -L/usr/local/lib -lSDL2 -o demo

To compile the following example, place a graphic.bmp file in the same location as the c file, or remove the image block.

#include <SDL2/SDL.h>
#include <stdio.h>

int
error(char* msg, const char* err)
{
	printf("Error %s: %s\n", msg, err);
	return 1;
}

int
main()
{
	SDL_Window* window = NULL;
	SDL_Surface* surface = NULL;
	SDL_Surface* image = NULL;

	if(SDL_Init(SDL_INIT_VIDEO) < 0)
	return error("init", SDL_GetError());

	window = SDL_CreateWindow("Blank Window",
	SDL_WINDOWPOS_UNDEFINED,
	SDL_WINDOWPOS_UNDEFINED,
	640,
	480,
	SDL_WINDOW_SHOWN);

	if(window == NULL)
		return error("window", SDL_GetError());

	surface = SDL_GetWindowSurface(window);
	SDL_FillRect(surface, NULL,
	SDL_MapRGB(surface->format, 0x72, 0xDE, 0xC2));

	/* Display an image */
	image = SDL_LoadBMP("graphic.bmp");

	if(image == NULL)
		return error("image", SDL_GetError());

	SDL_BlitSurface(image, NULL, surface, NULL);

	/* Draw canvas */
	SDL_UpdateWindowSurface(window);
	SDL_Delay(2000);

	/* close */
	SDL_FreeSurface(surface);
	surface = NULL;
	SDL_DestroyWindow(window);
	window = NULL;
	SDL_Quit();

	return 0;
}

Misc

String padding: |Hello |

printf("|%-10s|", "Hello");

Macros

#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define ABS(a) (((a) < 0) ? -(a) : (a))
#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
People use C because it feels faster. Like, if you build a catapult strong enough that it can hurl a bathtub with someone crouching inside it from London to New York, it will feel very fast both on take-off and landing, and probably during the ride, too, while a comfortable seat in business class on a transatlantic airliner would probably take less time but you would not feel the speed nearly as much. Erik Naggum

incoming tools moogle fractran vera modal plan9 c ulz format playground playdate