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.

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 -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"

Midi

cc -std=c89 -Wall midi.c -o midi

Play note G with velocity of 64.

#include <linux/soundcard.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

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

int
main(void)
{
  char* device = "/dev/midi2";
  unsigned char g_on[3] = {0x90, 0x43, 0x40};
  unsigned char g_off[3] = {0x80, 0x43, 0x00};
  int f = open(device, O_WRONLY, 0);
  if(f < 0)
    return error("Unknown", device);
  printf("Note ON\n");
  if(!write(f, g_on, sizeof(g_on)))
    return error("Note", "ON");
  sleep(2);
  printf("Note OFF\n");
  if(!write(f, g_off, sizeof(g_off)))
    return error("Note", "OFF");
  close(f);
  return 0;
}

Data Types

char0xff0-255
short0xffff0-65535

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;
}

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)))

Reserved Words

autoelselongswitch
breakenumregistertypedef
caseexternreturnunion
charfloatshortunsigned
constforsignedvoid
continuegotosizeofvolatile
defaultifstaticwhile
dointstruct_Packed
double   
— Submit an edit to ansi_c.htm(245 lines)

incoming(15): about tools seconth oscean nasu dotgrid moogle moogle left orca gyo noton chr format computer plan9 c

Last update on 14S09, edited 2 times. +6/14fh ----|-