C, Go, Dart

a side-by-side reference sheet

grammar and invocation | variables and expressions | arithmetic and logic | strings | regexes | dates and time | arrays | dictionaries | functions | execution control | concurrency | file handles | files | file formats | directories | processes and environment | libraries and namespaces | user-defined types | objects | reflection | macros | net and web | dom | unit tests | debugging and profiling

c go dart
version used
gcc 4.6 in POSIX environment 1.3 1.7
show version
$ gcc --version $ go version $ dart --version
implicit prologue #include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
grammar and invocation
c go dart
hello world $ cat hello.c
#include <stdio.h>

int main(int argc, char **argv) {
  printf("Hello, World!\n");

$ gcc hello.c

$ ./a.out
Hello, World!
$ cat hello.go
package main
import "fmt"

func main() {
  fmt.Printf("Hello, World!\n")

$ go build hello.go

$ ./hello
Hello, World!
$ cat hello.dart
void main() {
  print('Hello, World!');

$ dart hello.dart
Hello, World!

$ dart2js hello.dart

$ node out.js
Hello, World!
file suffixes
source, header, object file
.c .h .o .dart none .js
block delimiters
{ } { } { }
statement separator ; ; or sometimes newline

a new line terminates a statement when the last token on the line is
  (1) an identifier,
  (2) a numeric, character, or string literal,
  (3) one of the keywords
break, continue,
      fallthrough, or return,
  (4) one of
++, --, ), ], or }
end-of-line comment
// comment // comment // comment
multiple line comment /* comment line
another line */
/* comment line
another line */
/* comment line
another line /* nested comment */ */
variables and expressions
c go dart
variable /* if inside function, memory allocated on stack: */
int i;
int j = 3;

/* memory allocated on heap: */
int *ip = malloc(sizeof(int));
*ip = 7;
// memory allocated on stack:
var i int

// allocated on stack; type inferred from literal:
j := 3

// memory allocated on heap:
ip := new(int)
*ip = 7
free heap
free(ip); none; uses garbage collection none; uses garbage collection
global variable /* in foo.cpp, outside of any function: */
int x = 7;

/* to declare in bar.cpp: */
extern int x;
// foo.go:
package foo

var x = 7

// bar.go:
package bar
import foo

var y = foo.x + 3
uninitialized variable Stack variables and heap variables allocated with malloc have indeterminate values. Global and static variables and heap variables allocated with calloc are zero-initialized. Every type has a zero value. For numeric types it is zero and for strings it is the empty string.
constant /* usually preprocessor is used: */
#define PI 3.14
const Pi = 3.14
assignment i = 3; // defines variable of appropriate type:
i := 3

// variable must already be declared:
i = 3
parallel assignment none // define variables of appropriate type:
m, n := 3, 7

// x and y must already be declared:
x, y = 2, 8
swap int x = 1, y = 2, tmp;

tmp = x;
x = y;
y = tmp;
x, y = y, x
compound assignment arithmetic:
+= -= *= /= %=

<<= >>= &= |= ^=
+= -= *= /= %=

<<= >>= &= |= ^=
increment and decrement premodifiers:
++i --i

i++ i--
postmodifiers only; cannot be used in expressions:
i++ i--
++i --i

i++ i--
/* pointer types only: */
nil null
null test
ip == NULL
char *s1 = s2 || "was null";
conditional expression
x > 0 ? x : -x none x > 0 ? x : -x
arithmetic and logic
c go dart
boolean type
int bool bool
true and false
1 0 true false true false
0 0.0 NULL false everything except for the boolean true
logical operators
&& || ! && || ! && || !
relational operators
== != < > <= >= == != < > <= >= == != < > <= >=
integer type signed char 1+ byte
short int 2+ bytes
int 2+ bytes
long int 4+ bytes
long long int 4+ bytes
unsigned type unsigned char: 8+
unsigned short int 2 bytes+
unsigned int 2 bytes+
unsigned long int 4+ bytes
unsigned long long int 4+ bytes
uint8 (byte)
float type float 4 bytes
double 8 bytes
long double 16 bytes
arithmetic operators
+ - * / % + - * / % + - * / %
integer division
3 / 7 3 / 7 3 ~/ 7
integer division by zero
system dependent; process often sent a SIGFPE signal on Unix, process sent a SIGFPE signal IntegerDivisionByZeroException
float division
3 / (float)7 3 / float32(7) 3 / 7
float division by zero /* these are float values but not literals: */
inf, nan, or -inf
// these are float values but not literals:
+Inf, NaN, or -Inf

// to get the float values:
import "math"

math.Inf(1) math.Nan() math.Inf(-1)
// not literals:
Infinity, NaN, or -Infinity
power #include <math.h>

pow(2.0, 3.0)
import "math"

math.Pow(2.0, 3.0)
import 'dart:math' as math;

math.pow(2.0, 3.0)
sqrt #include <math.h>

include "math"

import 'dart:math' as math;

sqrt -1 #include <math.h>

/* nan */
double x = sqrt(-1.0);
import "math"

// NaN
x := math.Sqrt(-2.0)

import "math/cmplx"

// (0+1.41421356i)
z := cmplx.Sqrt(-2.0)
import 'dart:math' as math;

// NaN:
transcendental functions #include <math.h>

exp log log2 log10
sin cos tan
asin acos atan
include "math"

math.Exp math.Log math.Log2 math.Log10
math.Sin math.Cos math.Tan
math.Asin math.Acos math.Atan
import 'dart:math' as math;

math.log none none
math.sin math.cos math.tan
math.asin math.acos math.atan
transcendental constants #include <math.h>

import "math"

import 'dart:math' as math;

float truncation #include <math.h>
double d = 3.77;
long trunc = (long)d;
long rnd = round(d);
long flr = floorl(d);
long cl = ceill(d);
include "math"

x = 3.77

trunc := int(x)
flr := int(math.Floor(x))
cl := int(math.Ceil(x))

// (-3.14).floor() is -4 but -3.14.floor() is -3.
absolute value #include <math.h>  /* fabs() */

int i = abs(-7);
float x = fabs(-7.77);
include "math"

complex type float complex 8 bytes
double complex 16 bytes
long double complex 32 bytes
complex construction #include <complex.h>

double complex z;
z = 1.0 + 2.0 * I;
var z complex128 = 1.0 + 2.0i none
complex decomposition
real and imaginary component, argument, absolute value, conjugate
#include <complex.h>

double x;
double complex w;

x = creal(z);
x = cimag(z);
x = carg(z);
x = cabs(z);
w = conj(z);
import "math/cmplx"

var x float64
var w complex128

x = real(z)
x = imag(z)
x = cmplx.Phase(z)
x = cmplx.Abs(z)
w = cmplx.Conj(z)
random number
uniform integer, uniform float
/* Value between 0 and RAND_MAX inclusive: */
int n = rand();

/* Value in interval [0.0, 1.0]: */
float x = (float)rand()/(float)RAND_MAX;
import "math/rand"

n := rand.Intn(100)
x := rand.Float64()
import 'dart:math';

var rnd = new Random();
int n = rnd.nextInt(100);
int x = rnd.nextDouble();
random seed srand(17); import "math/rand"

import 'dart:math';

var rnd = new Random(17);
bit operators
<< >> & | ^ ~ << >> & | none ^ << >> & | ^ ~
c go dart
string type
char * string String
string literal
/* string in initialized data segment: */
char *s = "hello";

/* string in heap: */
char *s2 = strdup(s);

// raw string literal:
var s = 'don\'t say "no"';
var s2 = "don't say \"no\"";
var s3 = '''don't say "no"'
var s4 = """don't say "no\""""

raw string literals: r'foo' r"foo"
newline in string literal /* compiler concatenates literals
   separated by whitespace: */

char *s = "first line\n"
  "second line";
// backquote literals only:
let s := `first line
second line`
// triple quote literals only:
var s = '''first line
second line''';

var s2 = """first line
second line"""
string escapes \a \b \f \n \r \t \v \" \' \? \\
\o \oo \ooo \xhh \uhhhh \Uhhhhhhhh
Double quote literals only:

\a \b \f \n \r \t \v \\ \"
\ooo \xhh \uhhhh \Uhhhhhhhh
\b \f \n \r \t \v \" \' \$ \\
\xhh \uhhhh \u{h…}
expression interpolation none var count = 3;
var item = "BALL";

// 3 balls
print('$count ${item.toLowerCase()}s');
compare strings /* == and < compare memory addresses: */

strcmp("hello", "world") == 0
strcmp("hello", "world") < 0
"hello" == "world"
"hello" < "world"
"hello" == "world"
"hello".compareTo("world") < 0
string to number strtoimax strtol strtoll
strtoumax strtoul strtoull
strtof strtod strtold
import "strconv"

//2nd arg is base, 3rd arg is size of int in bits:
i, _ := strconv.ParseInt("17", 10, 32)

// 2nd arg is size of float in bits:
x, _ := strconv.ParseFloat("3.14", 32)
var n = int.parse("17");
var x = double.parse("3.14");
number to string import "strconv"

//3rd arg is precision after decimal point;
// 4th arg is size of float in bits:

strconv.FormatFloat(3.14, 'f', 4, 32)

// 2nd arg is base:
strconv.FormatInt(7, 10)
split none import "strings"

s := "foo bar baz"
parts := strings.Split(s, " ")
var a = "foo bar bar".split(' ');
none import "strings"

parts := []string{"foo", "bar", "baz"}
s := strings.Join(parts, " ")
['foo', 'bar', 'baz'].join(' ')
concatenate char *s1 = "hello";
char *s2 = " world";
char *s3 = (char *)calloc(strlen(s1) + strlen(s2) + 1,sizeof(char));
strcpy(s3, s1);
strcat(s3, s2);
"hello" + " world" "hello" + " world"
replicate none import "strings"

hbar := strings.Repeat("-", 80)
var hbar = "-" * 80;
extract substring char target[3];
char *source = "hello";
strncpy(target, source + 2, 2);
"hello"[2:4] "hello".substring(2, 4)
index of substring const char *s = "hello";
const char *p = strstr("hello", "ll");
p ? p - s : -1;
import "strings"

// zero-based index; -1 if not found:
strings.Index("hello", "ll")
format string char buf[100];
snprintf(buf, 100, "%s: %d", "Spain", 7);
translate case

to upper, to lower
char *s = strdup("hello");
int i;

for (i=0; i < strlen(s); ++i) {
  s[i] = toupper(s[i]);

for (i=0; i < strlen(s); i++) {
  s[i] = tolower(s[i]);
import "strings"

both sides, on left, on right
import "strings"

s := " lorem "
strings.Trim(s, " ")
strings.TrimLeft(s, " ")
strings.TrimRight(s, " ")
" lorem ".trim()
" lorem ".trimLeft()
" lorem ".trimRight()
pad char buf[100];
snprintf(buf, 100, "%-10s", "hello");
"lorem".padRight(10, " ")
"lorem".padLeft(10, " ")
strlen("hello") len("hello") // number of 16 bit runes:
character type char rune // 16 bits:
character literal none
character lookup
chr and ord new String.fromCharCodes([65])

// for Unicode points above U+FFFF codeUnitAt will
// return part of a surrogate pair
regular expressions
c go dart
metacharacters /* REG_BASIC: */
. [ ] \ * ^ $

. [ ] \ ( ) * + ? { } | ^ $
. [ ] \ ( ) * + ? { } | ^ $

use raw string (i.e. backtick) literals to avoid having to escape backslashes.
. [ ] \ ( ) * + ? { } | ^ $

use raw string (i.e. with r prefix) literals to avoid having to escape backslashes.
character class abbrevations /* matches any character; does not match newline if
   REG_NEWLINE is used: */


/* more character classes available in pcre library */
. \d \D \s \S \w \W . \d \D \s \S \w \W
anchors /* match beginning and end of string; match beginning and
   end of line if REG_NEWLINE is used: */

^ $
^ $ \A \b \B \z ^ $ \b \B
match test #include <regex.h>

regex_t rx;
int retval;
char *pat = "1999";
char *s = "It's 1999";

/* Use REG_NOSUB if 4th arg to regexec() is NULL */
if (retval = regcomp(&rx, pat, REG_EXTENDED | REG_NOSUB)) {

  char buf[200];

  regerror(retval, &rx, buf, 200);
  fprintf(stderr, "regex error: %s\n", buf);
else {

  if (regexec(&rx, "s, 0, NULL, 0) == 0) {

import "fmt"
import "regexp"

var rx = regexp.MustCompile("1999")
if (rx.MatchString("It's 1999.")) {
RegExp rx = new RegExp(r"1999");
String s = "It's 1999";

if (rx.hasMatch(s)) {
case insensitive match test #include <regex.h>

regex_t rx;
int retval;
char *pat = "lorem";
char *s = "Lorem";

if (retval = regcomp(&rx, pat, REG_EXTENDED | REG_ICASE)) {

  char buf[200];

  regerror(retval, &rx, buf, 200);
  fprintf(stderr, "Regex error: %s\n", buf);
else {

  if (regexec(&rx, s, 0, NULL, 0) == 0) {
    printf("case insensitive match\n");

import "fmt"
import "regexp"

var rx = regexp.MustCompile("(?i)lorem")
if (rx.MatchString("Lorem Ipsum")) {
  fmt.Println("case insensitive match")
RegExp rx = new RegExp(r"lorem", caseSensitive: false);
String s = "Lorem Ipsum";

if (rx.hasMatch(s)) {
  print("case insensitive match");
modifiers /* bit flags used in 3rd arg of regcomp(): */
// use (?i), (?m), ... to insert in regex:
i m s U
RegExp constructor takes two optional boolean named parameters:

substitution import "fmt"
import "regexp"

s := "do re mi mi mi"
var rx = regexp.MustCompile("mi")
fmt.Println(rx.ReplaceAllString(s, "ma"))
RegExp re = new RegExp(r'mi');
print("do re mi mi mi".replaceAll(re, "ma"));
group capture #include <regex.h>

regex_t rx;
int retval;

char *pat = "([0-9]{4})-([0-9]{2})-([0-9]{2})";
char *s = "2010-06-03";

if (retval = regcomp(&rx, pat, REG_EXTENDED)) {

  char buf[200];

  regerror(retval, &rx, buf, 200);
  fprintf(stderr, "Regex error: %s\n", buf);
else {

  /* first match is entire pattern */
  regmatch_t matches[4];

  if (regexec(&rx, s, 4, matches, 0) == 0) {

    char yr[5];
    regmatch_t rm = matches[1];

    /* rm_so and rm_eo contain index of start and end of
       match; they are set to -1 if unused */

    strncpy(yr, s + rm.rm_so, rm.rm_eo - rm.rm_so);
    yr[5] = '\0';

    printf("year is %s\n", yr);

RegExp rx = new RegExp(r'(\d{4})-(\d{2})-(\d{2})');
var md = rx.firstMatch("2010-06-03");

if (md != null) {
  var yyyy = md.group(1);
  var mm = md.group(2);
  var dd = md.group(3);
  print("year: $yyyy month: $mm day: $dd");
dates and time
c go dart
unix epoch type time_t
broken-down datetime type struct tm Time
current unix epoch time_t now;

if (time(&now) == -1) {
  perror("time failed");
current datetime import "time"

t := time.Now()
broken-down datetime to unix epoch
unix epoch to broken-down datetime
datetime to string
unix epoch to string
char *s;

/* ctime returns pointer to statically allocated memory */
if ((s = ctime(&now)) == NULL) {
  fputs("ctime failed.");
format datetime
parse datetime
date subtraction
add duration
date parts
time parts
build broken-down datetime
local time zone determination
time zone info
name and utc offset in hours
daylight savings test
nonlocal time zone
sleep #include <unistd.h>

/* seconds */
int retval = sleep(10);
if (retval != 0) {
  printf("interupted with %d s to go", retval);

/* microseconds */
if (usleep(10000)) {
  perror("usleep failed");
dur := time.Duration(10 * time.Second)

dur2 := time.Duration(10000 * time.Microsecond)
cpu usage #include <sys/times.h>
#include <unistd.h>  /* sysconf */

struct tms start, end;

double ticks_per_s = (double)sysconf(_SC_CLK_TCK);

clock_t start_wall = times(&start);

if (start_wall < 0) {
  fputs("times failed", stderr);
  return (1);

int i;
for (i = 0; i < 1000 * 1000 * 1000; ++i) {
  /* empty loop */

clock_t end_wall = times(&end);

if (end_wall < 0) {
  fputs("times failed", stderr);
  return (1);

clock_t wall = end_wall - start_wall;
clock_t user = end.tms_utime - start.tms_utime;
clock_t system = end.tms_stime - start.tms_stime;

printf("wall: %f s\n", wall / ticks_per_s);
printf("user: %f s\n", user / ticks_per_s);
printf("system: %f s\n", system / ticks_per_s);
c go dart
declare int a[10]; // values are zero-initialized:
var a [10]int

// slice of length 5; capacity 10:
a2 := make([]int, 5, 10)
allocate on stack /* contents of memory undefined: */
int a[10];
compiler decides location in memory
allocate on heap #include <stdlib.h>

/* memory zero-initialized: */
int *a = calloc(10, sizeof(int));
compiler decides location in memory
free heap
free(a); none; garbage collected
literal int a[] = {1, 2, 3}; a := []int{1, 2, 3} var a = [1, 2, 3];
none // number of elements in array or slice:

// number of elements that can be stored in allocated memory; runtime reallocates when needed:
a[0] a[0] a[0]
a[0] = 4; a[0] = 4 a[0] = 4;
out-of-bounds behavior undefined, possible SIGSEGV panic: index out of range

if compiler detects a problem the code won't compile
element index char *a[3] = {"foo", "bar", "baz"};
int loc = -1, i;

for (i = 0; i < 3; ++i) {
  if (strcmp("bar", a[i]) == 0) {
    loc = i;
a := []string{"foo", "bar", "baz"}
loc := -1

for i, val := range a {
  if val == "bar" {
    loc = i
var a = ["a", "b", "c"];
slice int *a2;

a2 = a[2];
a := []string{"a", "b", "c", "d", "e"}

// {"c", "d"}:
a2 := a[2:4]
var a = ["a", "b", "c", "d", "e"];

// ["c", "d"]:
List a2 = a.getRange(2, 4);
slice to end a := []string{"a", "b", "c", "d", "e"}

// {"c", "d", "e"}:
a2 := a[2:]
manipulate back a := []int{1, 2, 3}

a = append(a, 4)
num := a[len(a) - 1]
a = a[:len(a) - 1]
var v = a.removeLast();
manipulate front
concatenate a := []int{1, 2, 3}
a2 := []int{4, 5, 6}
a3 := append(a, a2...)
copy a := []int{1, 2, 3}

a2 := a
// also sets a[0] to 4:
a2[0] = 4

a3 := make([]int, len(a))
copy(a3, a)
// a[0] is unchanged:
a3[0] = 5
var a = ["a", "b"];

List a2 = new List<String>.from(a);
iterate over elements int a[10];

for (i = 0; i < 10; ++i ) {
  a[i] = i * i;
iterate over indices and elements
reverse import "sort"

a := []int{1, 2, 3}
var a = ["a", "b", "c"];

List a2 = new List<String>.from(a.reversed);
sort int
compare(const void *a, const void *b) {

  if (*(int *)a < *(int *)b) {
    return -1;
    else if (*(int *)a == *(int *)b) {
    return 0;
  else {
    return 1;

int a[5] = {6, 8, 10, 9, 7};

/* 2nd arg is array length; 3rd arg is element size */
qsort(a, 5, sizeof(int), &compare);
var a = [3, 1, 4, 2];
map none none var a = [1, 2, 3];

var a2 = new List.from(a.map((n) => n * n));
filter none none var a = [1, 2, 3];

var a2 = new List.from(a.where((n) => n > 1));
reduce none none var a = [1, 2, 3];

var sum = a.reduce((v, e) => v + e);
c go dart
declare gold := make(map[string]int)
literal gold := map[string]int{
  "France": 7,
  "Spain": 3,
var d = {"t": 1, "f": 0};
size d.length
lookup import "fmt"

gold["France"] = 7
fmt.Printf("France: %d\n", gold["France"])
update d["t"] = 2;
out-of-bounds behavior accessing nonexistent key returns null
is key present if key not present, val will contain zero value for type and ok will contain false:
val, ok = gold["Norway"]
delete delete(gold, "France") d.remove("f");
iterate d.forEach((k, v) {
keys and values as arrays List keys = d.keys;
List vals = d.values;
c go dart
define function int add(int n, int m) {
  return n + m;
func add(n int, m int) int {
  return n + m

// parameters can share type declaration:
func add(n, m int) int {
  return n + m
num add(num n, num m) {
  return n + m;

// parameter and return types optional:
add(n, m) {
  return n + m;
invoke function
add(3, 7) add(3, 7)
forward declaration of function float add(float x, float y);

/* if a function invocation is encountered before a
  declaration or a definition, the arguments and the
  return value are assumed to have type 'int' */

printf("%f\n", add(2.2, 3.5));

float add(float x, float y) {
  return x + y;
import "fmt"

// function can be invoked before definition
fmt.Printf("%f\n", add(2.2, 3.5))

func add(n float32, m float32) float32 {
  return n + m
overload function not permitted not permitted
nest function not permitted not permitted
default value for parameter none none
variable number of arguments #include <stdarg.h>

char* concat(int cnt, ...) {

  int i, len;
  va_list ap;
  char *retval, *arg;

  va_start(ap, cnt);
  for (i = 0, len = 0; i < cnt; i++) {
    len += strlen(va_arg(ap, char*));

  retval = calloc(len + 1, sizeof(char));

  va_start(ap, cnt);
  for (i = 0, len = 0; i < cnt; i++) {
    arg = va_arg(ap, char*);
    strcpy(retval + len, arg);
    len += strlen(arg);

  return retval;

char *s = concat(4, "Hello", ", ", "World", "!");
func concat(strs ...string) string {
  var ret = ""
  for _, str := range strs {
    ret += str

  return ret
named parameters none none
pass by value void use_integer(int i) {
  function body

int i = 7;

pass by address void use_iptr(int *i) {
  function body

int i = 7;

pass by reference not possible
return value return arg return arg. If return values have names and no arguments are provided to return the values assigned to the return variables are used.
no return value /* declare function void: */
void print_err(char *msg) {
  fprintf(stderr, msg);
multiple return values not permitted func divmod(m, n int) (int, int) {
  return m / n, m % n

q, r := divmod(7, 3)
named return values none func divmod(m, n int) (q, r int) {
  q = m / n
  r = m % n

q, r := divmod(7, 3)
anonymous function literal none
invoke anonymous function
function with private state int
counter() {
  static int n = 0;

  return ++n;
function as value int
add(int m, int n) {
  return m + n;

/* a function cannot be stored in a variable, but
  its address can */

int (* fp)(int, int) = &add;

printf("1 + 2: %d\n", (*fp)(1, 2));
execution control
c go dart
for int i, n;

for (i = 1, n = 1; i <= 10; ++i) {
  n *= i;
n := 1

for i := 1; i <= 10; i++ {
  n *= i;
int n = 1;

for (int i = 1; i <= 10; ++i) {
  n *= i;
if int signum;

if (i > 0) {
  signum = 1;
} else if (i == 0) {
  signum = 0;
} else {
  signum = -1;
if x > 0 {
  signum = 1
} else if x == 0 {
  signum = 0
} else {
  signum = -1
int signum;

if (i > 0) {
  signum = 1;
} else if (i == 0) {
  signum = 0;
} else {
  signum = -1;
single statement branch if (n < 0)
import "fmt"

// braces are mandatory:
if n < 0 {
if (n < 0)
while int i = 0;

while (i < 10) {
i := 0

for i < 10 {
int i = 0;

while (i < 10) {
switch switch (i) {
case 0:
case 1:
break continue break continue break continue
c go dart
start thread #include <pthread.h>

typedef struct {
  int id;
} payload;

thread(void* arg) {
  payload* pl = (payload*)arg;
  printf("the value is %d\n", pl->id);

pthread_t thr;
payload pl = {3};

if (pthread_create(&thr, NULL, &thread, (void*)&pl)) {
  printf("failed to create thead\n");
terminate current thread #include <pthread.h>

payload pl = {7};

terminate other thread #include <pthread>

pthread_t thr;
payload pl = {3};

if (pthread_create(&thr, NULL, &thread, (void*)&pl)) {
  printf("failed to create thead\n");


if (pthread_cancel(thr)) {
  printf("failed to cancel thread\n");
  exit (1);
list threads no portable way
wait on thread
create message queue
send message
receive message
file handles
c go dart
standard file handles stdin stdout stderr import "os"

os.Stdin os.Stdout os.Stderr
read line from stdin char *line = NULL;
size_t cap = 0;
ssize_t len;

/* if line is not NULL, it should be memory allocated by
   malloc and the size should be in cap. If size is not
   sufficient getline will call realloc on line */

len = getline(&line, &cap, stdin);

if (len == -1) {
  if (ferror(stdin)) {
    perror("getline err");
  else if (feof(stdin)) {
    fprintf(stderr, "end of file\n");
} else {

  /* use line here */

import "bufio"
import "os"

var line string
var err error

b := bufio.NewReader(os.Stdin)

line, err = b.ReadString('\n')

if err != nil {
} else {
  // use line here
write line to stdout /* returns EOF on error */
int retval = puts("Hello, World!");
import "os"

os.Stdout.WriteString("Hello, World!\n")
print('Hello, World!');
write formatted string to stdout
printf("count: %d\n", 7); import "fmt"

fmt.Printf("count: %d\n", 7)
open file for reading /* returns NULL on error */
FILE *f = fopen("/etc/hosts", "r");
raw, err := os.Open("/etc/hosts")
if err == nil {
  f := bufio.NewReader(f)
open file for writing /* returns NULL on error */
FILE *f = fopen("/tmp/test", "w");
open file for appending /* returns NULL on error */
FILE *f = fopen("/tmp/err.log", "a");
close file /* returns EOF on error */
int retval = fclose(f);
close file implicitly none defer
i/o errors Functions return values such as EOF, NULL, or -1 to indicate error. In some cases errors are not distinguished from end-of-file. The functions ferror() and feof() can be used to test a file handle.

The type of error is stored in
errno. strerror(errno) converts the errors code to a string and perror() writes its argument to stderr with sterror(errno).
read line char line[BUFSIZ];

if (fgets(line, BUFSIZ, f) == NULL) {
  if (ferror(stdin)) {
    perror("getline err");
  else if (feof(stdin)) {
    fprintf(stderr, "end of file\n");

} else {
  if ('\n' == line[strlen(line) - 1]) {

    /* use line here */

} else {
    fprintf(stderr, "long line truncated\n");
iterate over file by line
read file into array of strings
read file into string
write string /* returns EOF on error */
int retval = fputs("Hello, World!", f);
write line /* returns EOF on error */
int retval = fputs("Hello, World!\n", f);
flush file handle if (fflush(f) == EOF) {
  perror("fflush failed");
end-of-file test
get and set file handle position long pos;
if ((pos = ftell(f)) == -1) {
  perror("ftell failed");

/* 3rd arg can also be SEEK_CUR or SEEK_END */
if (fseek(f, 0, SEEK_SET) == -1) {
  perror("fseek failed");
open unused file #include <limits.h>  /* PATH_MAX */
#include <unistd.h>

char buf[PATH_MAX];

strcpy(buf, "/tmp/foo.XXXXXX");

/* terminal Xs will be replaced: */
int fd = mkstemp(buf);

if (fd != -1) {
  FILE *f = fdopen(fd, "w");

  if (NULL == f) {
    perror("fdopen failed");
  } else {
    /* use f */

} else {
  perror("mkstemp failed");
c go dart
file test, regular file test
#include <sys/stat.h>
#include <unistd.h>  /* access() */

struct stat buf;

if (access("/tmp/foo", F_OK) >= 0) {
  /* file exists */

if (stat("/tmp/foo", &buf) != 0) {
  perror("stat failed");
} else if (S_ISREG(buf.st_mode)) {
  /* file is regular */
file size
#include <sys/stat.h>

struct stat buf;

if (stat("/tmp/foo", &buf) != 0) {
  perror("stat failed");
} else {
  printf("size: %llu\n", buf.st_size);
is file readable, writable, executable #include <unistd.h>

if (access("/etc/hosts", R_OK) != 0) {
  printf("not readable\n");
if (access("/etc/hosts", W_OK) != 0) {
  printf("not writable\n");
if (access("/etc/hosts", X_OK) != 0) {
  printf("not executable\n");
set file permissions #include <sys/stat.h>

if (chmod("/tmp/foo", 0755) == -1) {
  perror("chmod failed");
copy file, remove file, rename file /* no copy function in standard library */

if (remove("/tmp/foo")) {
  perror("remove failed");

if (rename("/tmp/bar", "/tmp/foo")) {
  perror("rename failed");
create symlink, symlink test, readlink #include <limits.h>  /* PATH_MAX */
#include <sys/stat.h>
#include <unistd.h>

if (symlink("/etc/hosts", "/tmp/hosts") == -1) {
  perror("symlink failed");

struct stat sbuf;

if (stat("/tmp/hosts", &buf) != 0) {
  perror("stat failed");
} else if (S_ISLNK(buf.st_mode)) {
  /* file is symlink */

char pbuf[PATH_MAX + 1];

ssize_t size = readlink("/tmp/hosts", pbuf, PATH_MAX);

if (size >= 0 ) {
  pbuf[size] = 0;
  /* pbuf now contains null-terminated string
     with target path */

generate unused file name /* if first argument is NULL, path is in system temp
   directory. Caller should free() return value. */

char *path = tempnam("/tmp", "foo");
file formats
c go dart
c go dart
build pathname
dirname and basename #include <libgen.h>

/* return pointers to statically allocated memory
   which is overwritten by subsequent calls */

char *s1 = dirname("/etc/hosts");
char *s2 = basename("/etc/hosts");
import "path"

absolute pathname #include <limits.h>

char buf[PATH_MAX];

if (realpath("..", buf) == NULL) {
  perror("realpath failed");
else {
  /* use buf */
iterate over directory by file #include <dirent.h>

DIR *dir = opendir("/etc");
struct dirent *de;

while (de = readdir(dir)) {
  printf("%s\n", de->d_name);

glob paths #include <glob.h>

glob_t pglob;
int i;

glob("/etc/*", 0, NULL, &pglob);

for (i = 0; i < pglob.gl_pathc; ++i) {
  printf("%s\n", pglob.gl_pathv[i]);

make directory #include <sys/stat.h>

if (mkdir("/tmp/foo")) {
  fprintf(stderr, "mkdir err: %s\n", strerror(errno));
recursive copy
remove empty directory #include <unistd.h>

if (rmdir("/tmp/foo") == -1) {
  perror("rmdir failed");
remove directory and contents
directory test
#include <sys/stat.h>

struct stat buf;

if (stat("/tmp/foo", &buf) != 0) {
  perror("stat failed");
} else if (S_ISDIR(buf.st_mode)) {
  /* file is directory */
generate unused directory #include <limits.h>

char buf[PATH_MAX];

strcpy(buf, "/tmp/fooXXXXXX");

/* terminal Xs will be replaced: */
if (mkdtemp(buf) == NULL) {
  perror("mkdtemp failed");
} else {
  /* use buf */
system temporary file directory /* defined in <stdio.h> */
processes and environment
c go dart
signature of main int main(int argc, char **argv) {
first argument
pathname of executable
getopt #include <getopt.h>

/* 2nd value indicates whether option takes an argument */
static struct option long_opts[] = {
  {"debug", 0, NULL, 'd'},
  {"threshold", 1, NULL, 't'},
  {0, 0, 0, 0}

int debug = 0;
double threshold = 0.0;
char *file = NULL;

int ch;
int opti;
char *endptr;

while (1) {
  ch = getopt_long(argc, argv, "dt:", long_opts, &opti);
  if (-1 == ch) {

  switch (ch) {
  case 'd':
    debug = 1;
  case 't':
    threshold = strtod(optarg, &endptr);
    if (*endptr != 0) {
      fprintf(stderr, "expected float: %s\n", optarg);
    fprintf(stderr, "unexpected arg: %d\n", ch);

/* optind is index of 1st arg not consumed by getopt */
if (optind != argc - 1) {
  fputs("USAGE: foo [--multi] [--threshold=NUM] FILE\n",
else {
  file = argv[optind];
environment variable #include <stdlib.h>

char *home = getenv("HOME");
setenv("EDITOR", "emacs", 1);
iterate thru environment variables
get user id and name #include <unistd.h>  /* getlogin */

printf("uid: %d\n", getuid());
printf("username: %s\n", getlogin());
exit /* use 0 for success; 1 through 127 for failure */
executable test #include <unistd.h>

if (access("/bin/ls", X_OK) != 0) {
  printf("not executable\n");
external command /* retval of -1 indicates fork or wait failed.
   127 indicates shell failed */

int retval = system("ls -l *");
get pid, parent pid #include <unistd.h>

/* getpid() and getppid() have return type pid_t */
printf("%d\n", getpid());
printf("%d\n", getppid())
set signal handler #include <signal.h>

handle_signal(int signo) {
  switch(signo) {
  case SIGUSR1:
    puts("caught SIGUSR1");
    printf("unexpected signal: %s", strsignal(signo));

/*2nd arg can also be SIG_IGN or SIG_DFL */
sig_t prev_handler = signal(SIGUSR1, &handle_signal);

if (prev_handler == SIG_ERR) {
  perror("signal failed");
send signal #include <signal.h>
#include <unistd.h>  /* getppid */

if (kill(getppid(), SIGUSR1) == -1) {
  perror("kill failed");
libraries and namespaces
c go dart
load library /* The library must also be linked:

     $ gcc foo.o main.c

   If the library is in an archive:

     $ gcc -lfoo main.c

#include "foo.h"
import "foo"

// Only capitalized identifiers are visible:
var bar = foo.GetBar()
load library in subdirectory #include "lib/foo.h" import "lib/foo"
library path Add directory to path searched by #include directive:
$ gcc -I/home/fred/include foo.c

Add directory to path searched by -l (lowercase L) option:
$ gcc -L/home/fred/lib -lbar foo.c
The installation libraries are in the GOROOT directory. Additional directories can be listed in the GOPATH environment variable. The directories are separated by colons (semicolons) on Unix (Windows).

Each directory contains a
src subdirectory containing source code and a pkg/ARCH subdirectory containing compiled libraries.
declare namespace none // A package declaration must be first statement
// in every source file..

package foo
alias namespace
none import fu "foo"
unqualified import of namespace none import . "foo"
user-defined types
c go dart
typedef typedef int customer_id;
customer_id cid = 3;
enum enum day_of_week {
  mon, tue, wed, thu, fri, sat, sun

enum day_of_week dow = tue;
struct definition struct medal_count {
  const char* country;
  int gold;
  int silver;
  int bronze;
type MedalCount struct {
  country string
  gold int
  silver int
  bronze int
struct declaration struct medal_count spain;
struct initialization struct medal_count spain = { "Spain", 3, 7, 4};

struct medal_count france = {
  .gold = 8,
  .silver = 7,
  .bronze = 9,
  .country = "France"
spain := MedalCount{"Spain", 3, 2, 1}

france := MedalCount{
  bronze: 9,
  silver: 7,
  gold: 8,
  country: "France"}
struct member assignment spain.country = "Spain";
spain.gold = 3;
spain.silver = 7;
spain.bronze = 4;
france := MedalCount{}
france.country = "France"
france.gold = 7
france.silver = 6
france.bronze = 5
struct member access int spain_total = spain.gold + spain.silver + spain.bronze; france_total = france.gold +
  france.silver +
c go dart
c go dart
test type var n = 3;
if (n is int) {
  n += 1;

if (n is! String) {
  n += "s";
basic types dynamic
c go dart
include file /* search path include system directories: */
#include <stdio.h>

/* search path also includes directory of source file */
#include "foo.h"
add system directory $ gcc -I/opt/local/include foo.c
define macro #define PI 3.14
command line macro $ gcc -DPI=3.14 foo.c
undefine macro #undef PI
macro with arguments #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
strinfigy macro argument
concatenate tokens
conditional compilation #if defined(__WIN32)
  win32_prinft("%f\n", x);
  printf("%f\n", x);
macro operators The conditional of #if can contain integer literals and the following operators:

&& || !
== != < > <= >=
+ - * / %
<< >> & | ^ ~

In addition, the defined() operator can be used to test whether a macro is defined.

#ifdef FOO is a shortcut for #if defined(FOO)
net and web
c go dart
http get import "fmt"
import "io/ioutil"
import "net/http"

resp, err := http.Get("http://www.google.com")
if err == nil {
  defer resp.Body.Close()
  body, err := ioutil.ReadAll(resp.Body)
  if err == nil {
c go dart
unit tests
c go dart
unit test example $ sudo apt-get install check

$ cat > check_foo.c
#include <check.h>

START_TEST(test_foo) {
  fail_unless(0, "not true");

Suite *
suite_foo(void) {
  Suite *ste = suite_create("suite: foo");
  TCase *tc = tcase_create("case: foo");

  tcase_add_test(tc, test_foo);
  suite_add_tcase(ste, tc);

  return ste;

main(void) {
  int number_failed;
  Suite *ste = suite_foo();
  SRunner *sr = srunner_create(ste);

  srunner_run_all(sr, CK_NORMAL);
  number_failed = srunner_ntests_failed(sr);

  return (number_failed);

$ gcc -o check_foo check_foo.c -lcheck

$ ./check_foo
Running suite(s): foo
0%: Checks: 1, Failures: 1, Errors: 0
check_foo.c:4:F:foo:test_foo:0: not equal
equality assertion fail_unless(1 == 2, "integers not equal");

char *s = "lorem";
fail_unless(strcmp(s, "ipsum"), "strings not equal");
setup and teardown
unit test coverage
debugging and profiling
c go dart
check syntax $ gcc -fsyntax-only foo.c
flag for stronger warnings $ gcc -Wall foo.c
suppress warnings $ gcc -w foo.c
treat warnings as errors $ gcc -Werror foo.c
lint $ sudo apt-get install splint
$ splint foo.c
source cleanup
run debugger $ gcc -g -o foo foo.c
$ gdb foo
debugger commands
help, list source, (re)load executable, next, step, set breakpoint, show breakpoints, delete breakpoint, continue, backtrace, up stack, down stack, print, run, quit
> h
> file PATH
> n
> s
> i
> d NUM
> c
> bt
> up
> do
> p EXPR
> r [ARG1[, [ARG2 ...]]
> q
profile code does not work on Mac OS X
$ gcc -pg -o foo foo.c
$ ./foo
$ gprof foo
memory tool $ sudo apt-get install valgrind
$ gcc -o foo foo.c
$ valgrind foo
_______________________________________________________________ _______________________________________________________________ _______________________________________________________________


version used

The compiler version used for this cheatsheat.

show version

How to get the compiler version.

implicit prologue

Code which the examples in this sheet assume to have been executed.


A selection of commonly used symbols and macros from the standard C library and the headers in which they are defined according to POSIX:

errno.h stdlib.h stdio.h string.h time.h



Grammar and Invocation

hello world

How to write, compile, and run a "Hello, World!" program.

file suffixes

The suffixes used for source files, header files, and compiled object files.

block delimiters

How a block of statements is delimited.

statement separator

How statements are terminated.

end-of-line comment

The syntax for a comment which is terminated by the end of the line.


The // style comment first appeared in the C99 standard.

multiple line comment

The syntax for a comment which can span multiple lines.

/* */ style comments cannot be nested in C or Go.

Variables and Expressions


How to declare a variable.

free heap

How to free memory allocated on the heap.

global variable

How to declare a global variable.

uninitialized variable

What happens when reading from an uninitialized variable.


How to define a constant.


Multiple constants can be declared in this manner:

const (
  Pi = 3.14
  E = 2.718


The syntax for assigning a value to a variable.

parallel assignment

The syntax for parallel assignment.


How to swap the values in two variables.

compound assignment

The compound assignment operators.

increment and decrement

The increment and decrement operators.


The null literal and where the null value can be used.


A typical definition:

#define NULL (void *)0

null test

How to test whether a value is null.


The equivalent of the COALESCE function from SQL.


The short circuit or operator || can be used as a coalesce operator. However, in C, C++, and Objective C, NULL is identical to zero, whereas in databases they are two distinct values.

conditional expression

The syntax for a conditional expression.

Arithmetic and Logic

boolean type


The following definitions are common:

typedef int BOOL;
#define TRUE 1
#define FALSE 0

true and false

Literals for the boolean values true and false.


The following definitions are common:

typedef int BOOL;
#define TRUE 1
#define FALSE 0


Values which evaluate as false in the conditional expression of an if statement.

logical operators

The logical operators.

In all languages on this sheet the && and || operators short circuit: i.e. && will not evaluate the 2nd argument if the 1st argument is false, and || will not evaluate the 2nd argument if the 1st argument is true. If the 2nd argument is not evaluated, side-effects that it contains are not executed.

relational operators

Binary operators which return boolean values.

integer type

Signed integer types.


Whether char is a signed or unsigned type depends on the implementation.

unsigned type

Unsigned integer types.


Whether char is a signed or unsigned type depends on the implmentation.

float type

Floating point types.

arithmetic operators

The arithmetic binary operators.

integer division

How to find the quotient of two integers.

integer division by zero

The result of attempting to divide an integer by zero.


The behavior for division by zero is system dependent; the behavior described is common on Unix.

float division

How to perform float division on integers.

float division by zero

The result of attempting to divide a float by zero.


How to perform exponentiation.


The square root function.

sqrt -1

The result of attempting to find the square root of a negative nubmer.

transcendental functions

The exponential function; logarithm functions; trigonometric and inverse trigonometric functions.

transcendental constants

Constants for π and e.

float truncation

Functions for converting a float to a nearby integer value.


The math.h library also provides floor and ceil which return double values.

absolute value

The absolute value of a numeric.

complex type

Complex floating point types.

complex construction

How to create a complex number.


The C11 standard introduced macros for constructing complex numbers which work correctly when the arguments are inf, nan, or +nan:

     double complex CMPLX(double x, double y)
     float complex CMPLXF(float x, float y)
     long double complex CMPLXL(long double x, long double y)

complex decomposition

How to decompose a complex number into its real and imaginary parts; how to get the argument and absolute value of a complex number; how to get its complex conjugate.

random number

How to generate a random integer from a uniform distribution; how to generate a random float from a uniform distribution.

random seed

How to set the random seed.


There are at least three random number generators defined in stdlib.h. Each has its own function for setting the random seed:

srand(17);    /* used by rand() */

srandom(17);  /* used by random() */

srand48(17);  /* used by drand48() and lrand48() */

bit operators

The bit operations: right shift, left shift, and, or, exclusive or, and not.


Note that ^ is bit-not and not exclusive-or like in C.


string type

The type for a string

character type

The type for a character.

string literal

The syntax for a string literal.


The backquote literal is also called the raw string literal. It has no escape sequences, and in particular there is no way for it to contain a backquote character.

newline in string literal

Can newlines be included in string literals?


The compiler will convert the following three string literals to the single literal "foobarbaz".

char *metavars = "foo"

string escapes

Escape sequences in string literals.

expression interpolation

string length

string comparison


Returns 1, 0, or -1 depending upon whether the first string is lexicographically greater, equal, or less than the second. The variants strncmp, strcasecmp, and strncasecmp can perform comparisons on the first n characters of the strings or case insensitive comparisons.

to C string

numeric conversion


strtoimax, strtol, strtoll, strtoumax, strtoul, and strtoull take three arguments:

strtoimax(const char *str, char **endp, int base);

The 2nd argument, if not NULL, will be set to first character in the string that is not part of the number. The 3rd argument can specify a base between 2 and 36.

strtof, strtod, and strtold take three arguments:

strtod(const char *str, char **endp);


string join



extract substring

index of substring

format string

translate case



Regular Expressions


The list of regular expression metacharacters.

A regular expression that does not contain any metacharacters matches itself as a string.

character class abbrevations

Abbreviations for character classes.


regex.h (POSIX 2008)

We describe the regex library which is mandated by POSIX.

The PCRE library is available or easily installed on most systems and provides Perl style regular expressions. In particular PCRE has these character class abbreviations: \d \D \h \H \s \S \v \V \w \W.

To install PCRE on Ubuntu and read the documentation:

$ sudo apt-get install pcre

$ man pcre

To include the PCRE definitions in a C file:

#include <pcre.h>


Metacharacters for matching locations in the string which aren't single characters or substrings.

match test

How to test whether a string matches a regular expression.

case insensitive match test

How to test whether a string matches a regular expression in a case insensitive manner.


Modifers which can be used to customize the behvaior of a regular expression.


The meaning of the modifiers:

i case insensitive match
m ^ and $ match begin and end of line in addition to begin and end of string
s l. matches \n
U make (foo)*, (foo)+ non-greedy and (foo)*?, (foo)+? greedy


How to replace the part of a string matching a regular expression.

group capture

How to use a regular expression to parse a string.

Dates and Time

date/time type

date/time difference type

current date/time

unix epoch

convert to string


date parts

time parts

build date/time from parts



How to declare an array variable.

allocate on stack

How to allocate an array on the stack.

allocate on heap

How to allocate an array on the heap.

free heap

How to free an array that was allocated on the heap.


Syntax for an array literal.


How many elements are stored in an array.


How to get an element by index.


Arrays can be manipulated with pointer syntax. The following sets x and y to the same value:

int a[] = {3,7,4,8,5,9,6,10};
int x = a[4];
int y = *(a+4);


How to set or change the element stored at an index.

out-of-bounds behavior

What happens when an attempt is made to access an element at an invalid index.

element index


slice to end

manipulate back

manipulate front



iterate over elements

How to iterate over the elements of an array.


C arrays do not store their size; if needed the information can be stored in a separate variable. Another option is to use a special value to mark the end of the array:

char *a[] = { "Bob", "Ned", "Amy", NULL };
int i;
for (i=0; a[i]; i++) {
  printf("%s\n", a[i]);

iterate over indices and elements


How to reverse the elements of an array.


How to sort the elements of an array.





map declaration


For those interested in an industrial strength hashtable implementation for C, here is the header file and the source file for the hashtable used by Ruby.
For those interested in a "Computer Science 101" implementation of a hashtable, here is a simpler source file and header file.

map access

map size

map remove

map element not found result

map iterator


define function

How to define a function.

invoke function

How to invoke a function.

forward declaration of function

How to declare a function without defining it.

overload function

How to define multiple functions with the same name. The functions differ in either the number or type of arguments.

nest function

How to define a function inside another function.

default value for parameter

variable number of arguments


The stdarg.h library supports variable length functions, but provides no means for the callee to determine how many arguments were provided. Two techniques for communicating the number of arguments to the caller are (1) devote one of the non-variable arguments for the purpose as illustrated in the table above, or (2) set the last argument to a sentinel value as illustrated below. Both techniques permit the caller to make a mistake that can cause the program to segfault. printf uses the first technique, because it infers the number of arguments from the number of format specifiers in the format string.

char* concat(char* first,  ...) {

  int len;
  va_list ap;
  char *retval, *arg;

  va_start(ap, first);
  len = strlen(first);

  while (1) {
    arg = va_arg(ap, char*);
    if (!arg) {
    len += strlen(arg);


  retval = calloc(len+1,sizeof(char));

  va_start(ap, first);

  strcpy(retval, first);
  len = strlen(first);

  while (1) {
    arg = va_arg(ap, char*);
    if (!arg) {
    printf("copying %s\n", arg);
    strcpy(retval+len, arg);
    len += strlen(arg);


  return retval;

An example of use:

string *s = concat("Hello", ", ", "World", "!", NULL);

named parameters

pass by value

pass by address

pass by reference

return value

How the return value for a function is determined.

no return value

How to define a function with no return value.


The return value can be explicitly declared as Void:

func print_err(err: String) -> Void {

multiple return values

How to return multiple values.

named return values

How to return values by assigning values to variables.

anonymous function literal

invoke anonymous function

function with private state

function as value

Execution Control




The curly braces surrounding an if or else clause are optional if the clause contains a single statement. The resulting dangling else ambiguity is resolved by setting the value of c to 2 in the following code:

int a = 1;
int b = -1;
int c = 0;
if (a > 0)
if (b > 0)
  c= 2;



If the body of a while loop consists of a single statement the curly braces are optional:

int i = 0;
while (i<10)
  printf("%d\n", ++i);


A switch statement branches based on the value of an integer or an integer expression. Each clause must be terminated by a break statement or execution will continue into the following clause.



File Handles

standard file handles

The file handles for standard input, standard output, and standard error.


POSIX systems provide processes with the ability to open multiple files and manipulate them with via integers called file descriptors. Normally the integers 0, 1, and 2 refer to standard input, standard output, and standard error. The header <unistd.h> defines the macros STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO for these file descriptors.

System calls take file descriptors as arguments, but the C standard library provides an alternate set functions for buffered I/O. The standard library functions use FILE structs to identify streams and open files.

read line from stdin

How to read a line from standard input.

write line to stdout

How to write a line to standard output.

write formatted string to stdout

How to print a formatted string to standard out.


The printf man page describes the notation used in C style format strings.

open file for reading

How to open a file for reading.

open file for writing

How to open a file for reading.

open file for appending

How to open a file for appending.

close file

How to close a file handle.

close file implicitly

i/o errors

read line

iterate over file by line

read file into array of strings

read file into string

write string

write line

flush file handle

end-of-file test

get and set file handle position

open unused file

How to open a file with a previously unused file name.


The function mkstemps can be used to create a new file with a fixed suffix: "/tmp/fooXXXXXXsuffix".


file test, regular file test

Does the file exist; is the file a regular file.

file size

The size of the file in bytes.

is file readable, writable, executable

Can the process read, write, or executable the file?


access returns 0 if the process has the permission, and -1 if it doesn't or some other error occurred.

access uses the real user id to determine permission even though the kernel uses the effective user id.

set file permissions

copy file, remove file, rename file

create symlink, symlink test, readlink

generate unused file name

File Formats


build pathname

dirname and basename

absolute pathname

iterate over directory by file

glob paths

make directory

recursive copy

remove empty directory

remove directory and contents

directory test

generate unused directory

system temporary file directory

Processes and Environment

signature of main

first argument


The first argument is the pathname to the executable. Whether the pathname is absolute or relative depends on how the executable was invoked. If the executable was invoked via a symlink, then the first argument is the pathname of the symlink, not the executable the symlink points to.

environment variable

iterate thru environment variables


How to set the exit status and cause the process to exit.


On POSIX systems zero indicates success and other values indicate failure.

On Linux and Mac OS X the value returned to the parent is exit_arg & 0377. If the process exited because of a signal, the kernel sets the exit status to 128 plus the signal number. The signals are numbered starting from 1, leaving exit status values from 1 to 127 and perhaps 128 available for other failure conditions.

The C standard library defines the values EXIT_SUCCESS and EXIT_FAILURE as an aid for writing code which is portable to systems which do not use 0 to indicate success.

Libraries and Namespaces

load library

How to load a library.


Loading a library is a two part process.

Library declarations are made available by including headers in the client code.

Linking is performed by listing the library objects with the client object containing the main function on the command line when invoked the linker.

Alternatively, library objects can be collected into archive files using the ar command. These files have a .a suffix. The -l (lowercase L) option is used to include an archive when linking.


Multiple libraries can be loaded by listing them one after another:

import  "fmt"
import "math/rand"

Alternatively a single import statement can be used:

import (

load library in subdirectory

How to load a library in a subdirectory of the load path.

library path

How to the library path is specified.

declare namespace

How to declare the namespace of a source file.

alias namespace

How to import a namespace under an alias.

This can be used to provide an abbreviated name for a namespace.

It also allows a client to use two different libraries which declare the same namespace.

unqualified import of namespace

How to import all the identifiers in a library so that the client can refer to them without the namespace prefix.

User-Defined Types



Because C integer types don't have well defined sizes, typedef is sometimes employed to as an aid to writing portable code. One might include the following in a header file:

typedef int int32_t;

The rest of the code would declare integers that need to be 32 bits in size using int32_t and if the code needed to be ported to a platform with a 16 bit int, only a single place in the code requires change. In practice the typedef abstraction is leaky because functions in the standard library such as atoi, strtol, or the format strings used by printf depend on the underlying type used.



Enums were added to the C standard when the language was standardized by ANSI in 1989.

An enum defines a family of integer constants. If an integer value is not explicitly provided for a constant, it is given a value one greater than the previous constant in the list. If the first constant in the list is not given an explicit value, it is assigned a value of zero. it is possible for constants in a list to share values. For example, in the following enum, a and c are both zero and b and d are both one.

enum { a=0, b, c=0, d };

A typedef can be used to make the enum keyword unnecessary in variable declarations:

typedef enum { mon, tue, wed, thu, fri, sat, sun } day_of_week;
day_of_week d = tue;

From the point of view of the C compiler, an enum is an int. The C compiler does not prevent assigning values to an enum type that are not in the enumerated list. Thus, the following code compiles:

enum day_of_week { mon, tue, wed, thu, fri, sat, sun };
day_of_week d = 10;

typedef enum { mon, tue, wed, thu, fri, sat, sun } day_of_week2;
day_of_week2 d2 = 10;

struct definition

A struct provides names for elements in a predefined set of data and permits the data to be accessed directly without the intermediation of getters and setters. C++, Java, and C# classes can be used to define structs by making the data members public. However, public data members violates the uniform access principle.

struct declaration

struct initialization


The literal format for a struct can only be used during initialization. If the member names are not provided, the values must occur in the order used in the definition.

struct member assignment

struct member access


The period operator used for member access has higher precedence than the pointer operator. Thus parens must be used
to get at the member of a struct referenced by a pointer:

struct medal_count {
char* country;
int gold;
int silver;
int bronze;

struct medal_count spain = { "Spain", 3, 7 4 };
struct medal_count *winner = &spain;
printf("The winner is %s with %d gold medals", (*winner).country, (*winner).gold);

ptr->mem is a shortcut for (*ptr).mem:

printf("The winner (%s) earned %d silver medals", winner->country, winner->silver);




Net and Web

http get

How to make an HTTP GET request.


Unit Tests

Debugging and Profiling


ANSI C Standard (pdf) 1999
GNU C Library


-o name of output file


-E  stop after preprocessor; do not compile
-S  stop after compilation; do not assemble
-c  stop after assembly; do not link


-I          add directory to list of directories containing header files
-D FOO      define macro FOO as 1
-D FOO=VAL  define macro FOO as VAL
-U FOO      undefine macro FOO
-M          output make dependency info


-lfoo  search library foo for symbols when liking
-L     add directory to search path used by -l


-w       no warnings
-Werror  make warnings errors
-Wall    enable all warnings


-g           make object debuggable
-pg          generate executable which produces output for gprof
-O1 -O2 -O2  spend progressively more time in compilation optimizing code


-F  add a framework directory (used in place of -I, -l, -L)


Language Specification
Package Reference


Programmer's Guide
API Reference

issue tracker | content of this page licensed under creative commons attribution-sharealike 3.0