tags:

views:

280

answers:

7

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

#if 1
typedef unsigned short PronId;             /* uniquely identifies (word,pron) pair, i.e. 
                                   homophones have different Ids */
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?

+3  A: 

For experimenting with various code paths.

fortran
+7  A: 

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 test different algorithms.

James Curran
+13  A: 

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

KennyTM
+1  A: 

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.

mythz
+4  A: 

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.

Robert
+3  A: 

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.

Pavel Shved
+2  A: 

It is just a different way to comment out big piece of code.

Dennis Yurichev