Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly can you put after #if 0?

According to the C language standard, the lines between #if 0 and #endif are required only to contain preprocessing tokens, so most kinds of completely malformed syntax e.g. #foo or #include [bar] are allowed (silently ignored); GCC and Microsoft C++ do indeed silently ignore such.

An @ does not as far as I can see correspond to any preprocessing token so should still be a syntax error. GCC and Microsoft C++ silently ignore it. Is this effectively a language extension or am I missing something?

Does anyone actually use the ability to put malformed syntax between #if 0 and #endif in practice?

like image 743
rwallace Avatar asked Jan 07 '13 11:01

rwallace


1 Answers

Both the C and C++ standard contain a special 'escape clause' in their grammar that makes that every non-white-space character is (part of) a preprocessing token. For this reason, whatever you put in a block between #if 0 and #endif can almost never cause a compilation error. The only exception are mismatched quotes for character and string literals.

And yes, I regularly put malformed syntax between #if 0 and #endif to disable some partially-written code.

like image 180
Bart van Ingen Schenau Avatar answered Sep 28 '22 07:09

Bart van Ingen Schenau