§ Statement expressions and other GCC C extensions

This seems really handy. I've always loved that I could write
let x = if y == 0 { 1 } else { 42}
in Rust. It's awesome to know that the C equivalent is
const int x =  ({ if (y == 0) { return 1; } else { return 42; });

§ Conditions (?:) with omitted operands

x ?: y =defn= x ? x : y

§ variable length arrays

FILE *
concat_fopen (char *s1, char *s2, char *mode)
{
  char str[strlen (s1) + strlen (s2) + 1];
  strcpy (str, s1);
  strcat (str, s2);
  return fopen (str, mode);
  // str is freed here.                                
}

§ Designated array initializers: A better way to initialize arrays

  • initialize specific indexes
// initialize specific indexes
int a[6] = { [4] = 29, [2] = 15 };
// a[4] = 29; a[2] = 15; a[rest] = 0
  • initialize ranges:
// initialize ranges
int widths[] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };
  • initialize struct fields:
//initialize struct fields
struct point { int x, y; };
struct point p = { .y = yvalue, .x = xvalue };
  • initialize union variant:
//initialize union variant
union foo { int i; double d; };
union foo f = { .d = 4 };
  • Neat trick: lookup for whitespace in ASCII:
int whitespace[256]
  = { [' '] = 1, ['\t'] = 1, ['\h'] = 1,
      ['\f'] = 1, ['\n'] = 1, ['\r'] = 1 };

§ Cast to union

union foo { int i; double d; };
int x = 42; z = (union foo) x;
double y = 1.0; z = (union foo) y;

§ Dollar signs in identifier names

int x$;
int $z;

§ Unnamed union fields

struct {
  int a;
  union { int b; float c; };
  int d;
} foo

// foo.b has type int
// foo.c has type float, occupies same storage as `foo.b`.