Preprocessor

Mar 15, 2021 at 10:22am
What does this directive do?
I saw this somewhere
1
2
3
#if 0
   // some code
#endif  
Mar 15, 2021 at 10:28am
It comments out the block.
Mar 15, 2021 at 11:03am
If you want the code included for compile then change to:

1
2
3
#if 1
   // some code
#endif  

Mar 15, 2021 at 1:09pm
Thanks, I understand the idea, but why not use a comment for the purpose?
Mar 15, 2021 at 1:33pm
Hello thmm,

This may help explain better:
1
2
3
4
5
6
7
8
9
10
11
12
#define DEBUG

#ifdef DEBUG
    // Some code
#endif

Or 

#ifndef DEBUG
    // Some code
#endif

The first will include the code if DEBUG is defined and the second will exclude the code. By putting a comment on line 1 you can change what compiles and what does not.

In a larger program this is easier than going through the entire code to figure out what to comment and then what to uncomment.

It is like defining a constant variable like "MAXSIZE" then using it to define the size of an array or later in a for loop. When you need to change something you only have to change the value of "MAXSIZE" to effect everywhere it is used.

Andy
Mar 15, 2021 at 2:39pm
Thanks, I understand the idea, but why not use a comment for the purpose?


What happens if you code looks like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int function()
{
    int initialization = 0;

    initialization++;

    // In some situations we want to increment it again
    initialization++;

    /*
     * But after the second increment, we really need to multiply
     * the value by 2 to get the correct result
     */
    initialization *= 2;

    return initialization;
}


Now, you want to test the code without lines 7 - 14. How would you comment this out?

You can't simply put it in a block comment (/* */) because nested block comments don't work like you might want in C++. You could add line comments (//) to all of the lines from 7 - 14, but then line 7 gets a little bit messy--do you add an additional // or not?

You could put block comments around lines 7 & 8 and line 14, but that can become a headache (in a substantial program), and you have to make sure you clean up the comments properly at the end.

The simplest way to not compile the code is to add #if 0 .... #endif around lines 7 - 14.

Mar 15, 2021 at 3:25pm
The #if 0 .... #endif is very static; you have to edit the code if you want to compile the "other" version.
Quick and effective.

On the other hand if-statement is dynamic, determined during runtime and all the code is in the binary.
1
2
3
if ( cond ) {
  // code
}


Handy Andy did mention a compromise. Rather than static 0, have something, whose preprosessor evaluation can be affected during build (without modifying the source):
#ifdef COND


Only the context could tell why some code blocks are commented out in that static manner. One should also ask: Why comment out, when you could remove the code block and commit the new version into version control system?
Last edited on Mar 15, 2021 at 3:25pm
Mar 15, 2021 at 4:14pm
By the way, I agree with everything that @keskiverto and @Handy Andy said. I was directly answering the question that was asked and not intending to offer better alternatives.

I do occasionally (very occasionally) use #if 0, but only in the context of temporarily commenting out a block of code that I anticipate restoring in the not-too-distant future. I do this because, as @keskiverto said, it's quick and effective.

However, usually I define a macro like @Handy Andy suggested when I want to keep different build options alive over longer periods of time.

If the dynamic, run-time decision is desired, of course removing code via #if or comment blocks cannot be used.
Mar 15, 2021 at 4:15pm
Thanks everyone, now it's totally clear.
Topic archived. No new replies allowed.