Question

First of all, I'm aware of what exactly does an if 0 endif block do?.

But I was looking FreeBSD's cat.c and the explanation in the other question doesn't explain the #if 0 in this file. The blocks commented out are so small than someone could have easily removed them, but instead he/she chose to wrap them within #if 0 ... #endif. Why would someone make such a choice instead of simply removing this code? If it's used somehow as meta data, why not put it in some comment?

Was it helpful?

Solution

In this case, it is keeping a historical record of the version of the code from which the source was originally derived in the source code (so even if you don't have access to the version control system, you can see this information), while not making the information available to the compiler proper.

#if 0
#ifndef lint
static char const copyright[] =
"@(#) Copyright (c) 1989, 1993\n\
    The Regents of the University of California.  All rights reserved.\n";
#endif /* not lint */
#endif

Once upon a time, that information would have been compiled into the binary. Modern compilers eliminate unused variables like that.

OTHER TIPS

#if 0 is used instead of commenting out the code. One day instead of 0 they could use an environment variable to selectively enable/disable the functionality.

Sometimes we really do want to disable a block of code temporarily, without discarding it. This may be for many reasons.

Perhaps it's under development so we really don't want people using it yet, but we don't want to maintain separate copies of the code for that development than for release.

Or perhaps it's an idea that we tried that didn't work, but that we want to keep around while we figure out why it didn't work.

Or perhaps it's one of several alternatives that we've considered and we haven't made a final decision among them yet -- in that case you'll see things like #if 0 ... #else ... #endif

We could comment out the code we want to disable, but C before C99 didn't have the // comment-until-end-of-line syntax that C++ does, and that made commenting out a block that has comments painful at best. And while it's possible to do some switching back and forth between alternative blocks of code by abusing comments, it's ugly at best and is NOT the right answer for a language which has a C-like preprocessor.

(The addition of // comments is a fine thing, but lots of us old C coders don't use them and they still don't address the desire to easily switch between alternatives while testing out a new solution.)

We could use #ifdef. But someone might take that as a suggestion that they could try turning it on, and complain when it blows up. #if 0 says clearly "don't use this."

I agree that #if 0 is ugly. Unfortunately, it's also useful, and is a fairly common idiom. There's absolutely no requirement that you ever use it, but you should get used to seeing it because other folks will do so.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top