Tuesday 19 January 2016

Compile time ASSERT in 'C'

A nifty trick to get the compiler to check something the preprocessor can't grok.

{typedef int asserted[1-2*!condition];while(0==sizeof(asserted));}

The compiler can calculate array sizes based on things like enumerations that the pre-processor doesn't have access to, but will fail to parse an array declaration of negative length. No data or code is generated by this source line, just a freebie from the 'C' parser.

1 comment:

  1. Yes! Finally something about wells fargo online account login.

    ReplyDelete