Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why #if 0 vs #if (1 > 1)?

I am working with a legacy code and found this:

#if (1 > 1) //define some function #endif 

Not sure, how this can be any different from the more typical #if 0, to comment out the code? Any thoughts?

like image 900
MIbrah Avatar asked May 05 '16 21:05

MIbrah


1 Answers

Both expressions are false, so the code is never compiled.

Here are potential explanations for why the programmer did not want to use the obvious #if 0 preprocessor directive to disable a section of code:

  • the programmer did not want grep '#if 0' to find his code snippet.
  • the local coding conventions disallow #if 0 and possibly enforce this rule with a script. The programmer found a contorted workaround.
  • some programmer's editors (eg vim) colorize #if 0 sections as comments, using a different preprocessor expression defeats this.
  • the programmer might have thought a boolean expression was required after #if. The use of parentheses supports this explanation, but only the programmer can tell.
  • the original code had #if (OPTION > 1) and OPTION was changed to 1 with a sed script or some other global text replacement method.
  • the programmer may have wanted to attract the next reader's attention. Either for a humorous purpose or some other obscure goal. Goal achieved in this case.
  • as noted in some of the comments, this could be a lame attempt at obfuscating the code, to increase job security... For this purpose, I suggest the gets operator: #if (0 <- 1) or the crawling adder: #if (1 <~~ 1).
like image 102
chqrlie Avatar answered Sep 24 '22 03:09

chqrlie