Why would somebody use an #if 1 C preprocessor directive?
Asked Answered
V

9

23

I am looking through some C source code and I don't understand the following part

#if 1

   typedef unsigned short PronId;
   typedef unsigned short LMId;
#  define LM_NGRAM_INT

#else

   typedef unsigned int LMId;
   typedef unsigned int PronId;
#  undef LM_NGRAM_INT

#endif

Why would someone do #if 1? Isn't it true that only the first block will ever be processed?

Vevay answered 15/2, 2010 at 13:3 Comment(1)
@Thomas No, it is short for pronounciation id in a speech recognition applicationVevay
L
29

Yes.. Only the first block will be processed --- until someone changes the 1 to a 0. Then the other block will be compiled. This is a convenient way to temporary switch blocks of code in and out while testing different algorithms.

Lilybelle answered 15/2, 2010 at 13:6 Comment(0)
S
18

So that one can quickly choose which part to compile by changing the #if 1 to #if 0.

Skeptical answered 15/2, 2010 at 13:6 Comment(0)
D
8

One of the fundamental properties of software is that computer program is cheap to modify.

That's why certain code is written in such a way that it will make modification easier. That's why they need various patterns, like "interface", or "proxy".

And that's why you sometimes see weird constructs like #if 1-#else-#endif, an only purpose of which is to easily switch the part of code that will be compiled, by small effort: changing 1 to 0.

Disease answered 15/2, 2010 at 13:13 Comment(1)
I love the quote : Computer program is cheap to modifyDotdotage
M
5

I put that in my code when I need to test different set of parameters. Usually my product will ship with different defaults than what I can work with in a debug environment, so I put the shipping defaults in a #if 1 and the debug defaults in the #else with a #warning to warn me it's being built with debug defaults.

Mutz answered 15/2, 2010 at 13:8 Comment(0)
M
4

For experimenting with various code paths.

Mohur answered 15/2, 2010 at 13:6 Comment(0)
A
3

It is just a different way to comment out big piece of code, so, editor auto indentation would not break indentation (commented block of code would be indented as text, not as code).

Achievement answered 11/4, 2010 at 12:52 Comment(0)
F
2

I'm actually using it as a kludge to make code folding easier; if I wrap a section of code in an #if 1 ... #endif, I can fold it in my editor. (The code in question is very macro-heavy, and not written by me, so more traditional ways of making a huge block of code manageable won't work.)

Foot answered 31/7, 2013 at 14:59 Comment(0)
I
2

The cleaner way of doing it is probably doing something like:

#if ALGO1

#else

#endif

But, you will have to pass in ALGO1 to the compiler args somewhere...for example in a makefile, you need to add -DALGO1=1 (if no 1 is provided, 1 is assumed). Ref: http://www.amath.unc.edu/sysadmin/DOC4.0/c-compiler/user_guide/cc_options.doc.html

This is more work...so, usually, for quick checks, #if 1 is used. And in some cases, forgotten and left behind as well :-)

Infra answered 31/7, 2013 at 16:2 Comment(0)
E
1

It's another way of saying for #if true it was most likely a result of code that was previously checking for another symbol then refactored to always be true.

Erland answered 15/2, 2010 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.