Click here to Skip to main content
15,879,035 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am playing around with an example about freetype use here.
there is one macro statement I am not sure how it works:

#if 0
            // This is unused in this test but you would need this to draw
            // more than one glyph.
            float bearingX = face->glyph->metrics.horiBearingX >> 6;
            float bearingY = face->glyph->metrics.horiBearingY >> 6;
            float advance = face->glyph->advance.x >> 6;
#endif


my understanding is: 0 is false. so #if 0 is always false and the statements after it will not get executed. as this comment says, it is used only as a note for other usage?

so what is the benefit of using #if 0? only as a note for other usage?

What I have tried:

I understand how macro statement works, but try to clarify my understanding on this usage.
Posted
Updated 2-May-22 4:56am

Quote:
so what is the benefit of using #if 0? only as a note for other usage?

the benefit is that a single source code file can have more than 1 usage.
"#if" is a preprocessor command for conditional compilation.
Example:
- C/C++ have no language provision for modern things like unit testing or debugging. With preprocessor directives, the conditional code can be included/excluded as you want.
- It allow keeping different versions of your code, as you refine it and want to keep old versions.

- In order to test correctness of your code, you need special code that you don't want in production code. Conditional compilation is the simple solution.
- You have crafted some code to solve a complicated problem and want to refine the solution, you can either trow away first solution and start a new 1, but you can't use first solution since you killed it, either you use conditional compilation to switch between solution s as you need.
 
Share this answer
 
Comments
Southmountain 25-Jun-21 18:24pm    
thank you for the answer!
As you note, #if 0 works similar to a block comment. There are several uses for it:
1) It can be used annotate usage or additional features (as here)
2) It can be used when debugging to narrow down the area of the code that's causing the problem
3) It can be used to preserve code for documentation reasons
4) It might be used to indicate future development plans

The advantage to an #if 0/#endif block, is that it can create the equivalent of a block comment, even if the included code contains comments using the /* */ idiom
 
Share this answer
 
Comments
Greg Utas 25-Jun-21 14:57pm    
Good examples!
Southmountain 25-Jun-21 18:24pm    
now I get it clearly!
The pre-writer are correct but I add some important information to you: the #if isnt the normal way, but some "hack" which makes the code weired and problematic when used a lot. Better is to pack the whole code in a function or an normal if block which also has the else path.

C++
bool executeWeiredStrategy = true;

if( executeMyCode ) {
  // do your stuff
  deliverMoreWeapons();
} else {
  startPeaceTalks();
}
See C++ if statement tutorial.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900