alf.nu / @steike

Evil C

The "goes toward" operator'

void doStuff(int count) {
  while(count --> 0) {
     fleh();
  }
}

This was later popularized by ECMAScript.

The cast-to-bool operator

Node *left, *right;
int childCount() {
   return !!left + !!right;
}

Optimization, the Microsoft way

This was found in the 2004 leaked Windows sources

__inline BOOL
SearchOneDirectory(
                  IN  LPSTR Directory,
                  IN  LPSTR FileToFind,
                  IN  LPSTR SourceFullName,
                  IN  LPSTR SourceFilePart,
                  OUT PBOOL FoundInTree
                  )
{
    //
    // This was way too slow. Just say we didn't find the file.
    //
    *FoundInTree = FALSE;
    return(TRUE);
}

Duff's Device for loop unrolling"

switch(c&3) while((c-=4)>=0) {
        foo(); case 3:
        foo(); case 2:
        foo(); case 1:
        foo(); case 0:
}

Struct/class offsets

int ofs = (int)&((Class*)0)->element;
Vilhelm S. comments: Note that <stddef.h> provides an offsetof(type, field_name) macro, so you can leave the dirty work of abusing NULL pointers in perverted ways to your standard library implementor! (The typical implementation of it is as above, though...)

Fun with comments

This font definition has a bug. Find it.

char font[] = {
....
0x00,0x61,0x51,0x49,0x45,0x43,0x00,0x00, // 1 :0x5A Z
0x70,0x29,0x24,0x22,0x24,0x29,0x70,0x00, // 1 :0x5B [
0x00,0x3D,0x42,0x42,0x42,0x42,0x3D,0x00, // 1 :0x5C \
0x00,0x3C,0x41,0x40,0x41,0x40,0x3C,0x00, // 1 :0x5D ]
...
Bernd Jendrissek writes: OMG I can't believe you stole code from my ex-job! I actually bumped into the font definition problem... except that two compilers behaved differently! TopSpeed C and GCC, in particular. The former didn't respect the backslash as a line continuation of a comment, so the code worked. Can't blame it too much, I think that compiler predates C99.

Endian magic

// this is from GMP
#define BYTE_OFS(x) (((char *)&internalEndianMagic)[7-(x)])
static double internalEndianMagic = 7.949928895127363e-275;

Another micro-optimization

"If x or y is less than 0"

if((x|y) < 0) { ... }
Benoit Hudson comments: Thankfully, gcc -- usually the lowest common denominator among compilers -- implements this optimization. So it's all right to beat your minion programmers with a stick when they do this by hand.

Dan's lexer tester

/* Test whether compiler supports C++-style comments */
#define HELPER 0//**/ 
#define CPLUSPLUS_COMMENTS_SUPPORTED (HELPER+1)

[] is symmetric

int direction = 1;
char direction_name = direction["nsew"];

snoob

Given an integer with n bits set, return the next higher number with the same number of bits set. From Hacker's Delight.

unsigned snoob(unsigned x) {
   unsigned smallest, ripple, ones;
                                // x = xxx0 1111 0000
   smallest = x & -x;           //     0000 0001 0000
   ripple = x + smallest;       //     xxx1 0000 0000
   ones = x ^ ripple;           //     0001 1111 0000
   ones = (ones >> 2)/smallest; //     0000 0000 0111
   return ripple | ones;        //     xxx1 0000 0111
}

List comprehensions, Java style

return new ArrayList<String>() {{ for(String x : foo) add(x.toUpper(); }};

Complaints to @steike or @steike.