Friday, March 05, 2010

Funny C tricks

Taken from Bill Rowan's Stanford ACM presentation:

The "downto" operator:

int main()
{
int i = 5;
while(i --> 0) // --> is the downto operator!
{
printf("%d\n", i);
}
return 0;
}

Cast any type to "bool" type (that is, 1 or 0):
!!foo
"Computed goto" (compiler-dependent && unary operator):
void print_loop(int s, int e) {
assert(s < e);
top:
printf("%d\n", s);
goto *( &&top + ( !!(s++/e) ) * ( &&end - &&top ) );
end:
;
}

No comments: