Why many people will use #if 1 and #if 0 in C code?. What to do to compile that code? 
Author Message
 Why many people will use #if 1 and #if 0 in C code?. What to do to compile that code?

Guys,

Why many people will use #if 1 and #if 0 in C code?. Does it proper
way of coding?. What to do to compile that code?. Thanks in advance.
For example:

#if 1
    X = X + 1;
#endif

#if 0
  Y = Y + 1;
#endif
--



Mon, 12 Sep 2005 03:40:08 GMT  
 Why many people will use #if 1 and #if 0 in C code?. What to do to compile that code?

Quote:

> Why many people will use #if 1 and #if 0 in C code?

Because they want to, and they can ;-)

Quote:
> Does it proper way of coding?

Yes. #if 0 ... #endif serves to comment out whole sections of code
without having to worry about nested /* */ comments.  #if 1 either
does same for its #else branch, or it started out as an #if 0 that
somebody decided they wanted to reactivate, yet keep the chance of
disabling it again.  

Doing this for single lines, as shown in your example, would be quite
silly, though, IMHO.

Quote:
> What to do to compile that code?

Nothing particular; just run it through the compiler as usual.
--

Even if all the snow were burnt, ashes would remain.
--



Tue, 13 Sep 2005 14:17:44 GMT  
 Why many people will use #if 1 and #if 0 in C code?. What to do to compile that code?
most use it as a way of commenting out lines.  a practice that
confuses most editors that fontify c/c++...

i.

Quote:

> Guys,

> Why many people will use #if 1 and #if 0 in C code?. Does it proper
> way of coding?. What to do to compile that code?. Thanks in advance.
> For example:

> #if 1
>     X = X + 1;
> #endif

> #if 0
>   Y = Y + 1;
> #endif

--



Tue, 13 Sep 2005 14:17:50 GMT  
 Why many people will use #if 1 and #if 0 in C code?. What to do to compile that code?

Quote:
> Why many people will use #if 1 and #if 0 in C code?. Does it proper
> way of coding?. What to do to compile that code?. Thanks in advance.
> For example:

> #if 1
>     X = X + 1;
> #endif

I never use this, but...

Quote:
> #if 0
>   Y = Y + 1;
> #endif

This is a good way to comment out sections of code which may include
other /* ... */ type comments, since nested comments aren't a) legal C
or b) not handled very well by some preprocessor/compilers. I'm not
sure if nested comments are legal C (and I'm too lazy to look it up)
but after running into some wierd compiler errors in the past because
of them, I just started using the #if 0 ... #endif for temporary
blocking out of unwanted code.

--
burton samograd

http://kruhftwerk.dydns.org
--



Tue, 13 Sep 2005 14:17:55 GMT  
 Why many people will use #if 1 and #if 0 in C code?. What to do to compile that code?
Typical uses of are to comment out code that either:

- isn't complete yet
- is almost obsolete, but wants to stay around for a while longer

And then my personal one:

Sometimes if I have a function that uses a complex structure, I'll cut
and paste the structure declaration from the header and use it as
comments.  Since I want to preserve the indentation and emacs does
automatic indentation, the #if 0 works better.

When you compile #if 0 means "don't include" and #if 1 means "include
it."

Quote:

> Why many people will use #if 1 and #if 0 in C code?. Does it proper
> way of coding?. What to do to compile that code?. Thanks in advance.
> For example:

> #if 1
>     X = X + 1;
> #endif

> #if 0
>   Y = Y + 1;
> #endif
> --


--
--

--



Tue, 13 Sep 2005 14:17:57 GMT  
 Why many people will use #if 1 and #if 0 in C code?. What to do to compile that code?

Quote:

> Why many people will use #if 1 and #if 0 in C code?. Does it proper
> way of coding?. What to do to compile that code?.

Usually it is because there are two alternatives and sometimes
the programmer wants to try one of them, sometimes the other.
For a single location, as opposed to repeated occurrences, it
is expedient to simply #if 0 the disabled code etc.  If there
are multiple places where a consistent choice of the alternative
should be made, then it is much better to use a single macro:
        #define DEBUG 1 /* enable debugging print-out */
        ...
        #if DEBUG
                fprintf(stderr, "entering foo()\n");
        #endif
        ...
--



Tue, 13 Sep 2005 14:18:03 GMT  
 Why many people will use #if 1 and #if 0 in C code?. What to do to compile that code?

Quote:

> Guys,

> Why many people will use #if 1 and #if 0 in C code?. Does it proper
> way of coding?. What to do to compile that code?. Thanks in advance.

This is just a way to comment out large portions of code.
As you probably know, /* */ comments cannot be nested, so putting them
around the code that itself has such comments wont work properly.

--
Maciej Pietrzak <magh na shnet pl>
"Oh my god! We all gonna die!"
--



Tue, 13 Sep 2005 14:18:09 GMT  
 Why many people will use #if 1 and #if 0 in C code?. What to do to compile that code?

Quote:
> Guys,

> Why many people will use #if 1 and #if 0 in C code?. Does it proper
> way of coding?. What to do to compile that code?. Thanks in advance.
> For example:

> #if 1
>     X = X + 1;
> #endif

> #if 0
>   Y = Y + 1;
> #endif

As to why people do it, #if 0 is often used to quickly comment out
a section of code, and changing that line to #if 1 is a quick way to
allow that section of code to be compiled.   I usually see this sort of
thing
in response to a quick debugging effort, where a coder is required
to quickly isolate a bug and solve it.

As to whether it's "proper", there are probably nearly as many opinions
as there are people who will comment.   I *personally* prefer to change
these sorts of things into (for example) an #ifdef DEBUG, and use
a command line option to selectively cut out sections of code from
the compiler.

#if 0, as I said above, is basically a quick way to comment out a block
of code, but it can be used as an alternate to // or /* */ styles of
comment.    The preprocessor basically prevents the compiler from
seeing that code.

#if 1 basically does nothing (1 is true, so the compiler will always see
code bounded by  #if 1   #endif after the preprocessor has done its stuff).
--



Tue, 13 Sep 2005 14:18:11 GMT  
 Why many people will use #if 1 and #if 0 in C code?. What to do to compile that code?

comp.lang.c.moderated:

Quote:
> Guys,

> Why many people will use #if 1 and #if 0 in C code?. Does it proper
> way of coding?. What to do to compile that code?. Thanks in advance.
> For example:

> #if 1
>     X = X + 1;
> #endif

> #if 0
>   Y = Y + 1;
> #endif

Comments in C do not nest.  Suppose that for some reason you want to
eliminate a section of code, to replace it with new code, but for one
reason or another you do not want to actually delete the text from the
source code file.

Some coders try to do something like this:

/* add a comment start before the code to eliminate
   if (some_condition)
   {
      do_something;
      do_something_else;
      and_one_more_thing;
   }
now add an end of comment */

There can be problems with this approach.  First, many organizations
have coding standards that forbid commenting out code.  Second, say
that the middle line inside the if-block already had a comment on it,
in other words instead of:

   do_something_else;

....it held:

   do_something_else; /* allocate memory */

Since C comments do not next, the "*/" at the end of the line above
would end the comment in the middle of the block.  The rest of the
if-block would be compiled and cause an error at the mis-matched end
of the comment block if not before.

On the other hand, you can temporarily or permanently keep code from
being compiled by placing:

#if 0
#endif

....around it, regardless of whether it contains comments or not.

One reason to do this at an early stage of program development is to
avoid calling functions that haven't been implemented yet:

   sprintf(buffer, "New Value %d\n", some_value);

#if 0
    Add_To_Log(buffer);  /* real function, not written yet */
#else
    printf("Logging string: %s", buffer);
#endif

While waiting for another programmer on the project to finish the
Add_To_Log() function, I can use printf() to display the result to
verify that my code is generating the correct value.

Later, when the Add_To_Log() function is available, I have one of two
choices:

1.  I can delete 4 of the 5 lines above, and just leave the one that
calls the Add_To_Log() function.

2.  I can change "#if 0" to "#if 1" in the first line, and leave
everything else alone.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
--



Tue, 13 Sep 2005 14:18:38 GMT  
 Why many people will use #if 1 and #if 0 in C code?. What to do to compile that code?

Quote:

> Why many people will use #if 1 and #if 0 in C code?. Does it proper
> way of coding?. What to do to compile that code?. Thanks in advance.
> For example:

> #if 1
>     X = X + 1;
> #endif

> #if 0
>   Y = Y + 1;
> #endif

There can be several reasons:

Often, they are additions/removal where the developer is not quite sure
if that is the right thing to do, except that it makes the program work
correctly.

Quote:
>From there, a second reason evolves: people forget about temporariness

of the changes and never go back to clean up the source.

Personally, I also use #if 0 for diagnostic code segments which are so
overhead-incuring that they are needed only in extreme cases (like
printing every executed instruction in an interpreter), or to supply
alternative implementation outlines for tricky problems, including a
comment why I did not use it.
--



Tue, 13 Sep 2005 14:18:39 GMT  
 Why many people will use #if 1 and #if 0 in C code?. What to do to compile that code?

Quote:
> Guys,

> Why many people will use #if 1 and #if 0 in C code?. Does it proper
> way of coding?. What to do to compile that code?. Thanks in advance.
> For example:

> #if 1
>     X = X + 1;
> #endif

> #if 0
>   Y = Y + 1;
> #endif

This is perfectly valid, but not very clear, code. #if 1/#endif does
nothing, while #if 0/#endif excludes everything between them from the
compilation.

A programmer typically uses #if 0 to comment out code that should no longer
be accessed, e.g. test code, code that was translated to Assembly language,
etc. When the code is re-instated, it is often easier to change the #if 0 to
#if 1 than to look for the closing #endif and delete both. This is bad
practice - it clutters up the code, but it is not illegal.

If a programmer needs to include differnet sections of code based of some
condition, he should define appropriate pre-processor definitions, and use
the #if defined() form to test for the presence of the definitions.

HTH,
Daniel Pfeffer
--



Tue, 13 Sep 2005 14:18:41 GMT  
 Why many people will use #if 1 and #if 0 in C code?. What to do to compile that code?
Santa,

It's a fairly common way of temporarily disabling chunks of code. The
++X below will be included, the ++Y won't.

The reason for doing it with #if is to prevent problems where the code
one wants to skip includes comments - remember that comments don't
nest. If you had
  (void) printf("A\n");
  /*comment*/
  (void) printf("B\n");
and tried to comments thaht chunk out with /* before and */
afterwards, the line to print Z would still be included and the */
after it would cause an error.

C.David

Quote:

> Guys,

> Why many people will use #if 1 and #if 0 in C code?. Does it proper
> way of coding?. What to do to compile that code?. Thanks in advance.
> For example:

> #if 1
>     X = X + 1;
> #endif

> #if 0
>   Y = Y + 1;
> #endif

--



Tue, 13 Sep 2005 14:18:43 GMT  
 Why many people will use #if 1 and #if 0 in C code?. What to do to compile that code?

Quote:
>Why many people will use #if 1 and #if 0 in C code?. Does it proper
>way of coding?. What to do to compile that code?. Thanks in advance.
>For example:

>#if 1
>    X = X + 1;
>#endif

>#if 0
>  Y = Y + 1;
>#endif

What would YOU do if you had to exclude a number of consecutive lines of
code from compilation, while still allowing the possibility of re-enabling
them laterr?  Keep in mind that some of them might contain comments and
that C comments do not nest.

Dan
--
Dan Pop
DESY Zeuthen, RZ group

--



Tue, 13 Sep 2005 14:18:45 GMT  
 Why many people will use #if 1 and #if 0 in C code?. What to do to compile that code?


Quote:
>Guys,

>Why many people will use #if 1 and #if 0 in C code?. Does it proper
>way of coding?. What to do to compile that code?. Thanks in advance.
>For example:

>#if 1
>    X = X + 1;
>#endif

>#if 0
>  Y = Y + 1;
>#endif

I think that 'many' is a considerable overstatement. It is actually a
long time since I came across that in new code rather than legacy code.
Event then it tended to be wrapped round a substantial block of code.
The idea was that the block of code could be switched in or out by
simply changing the conditional.

It is much more common to write things like:

#if COLOR_MONITOR
...
#else
...
#endif

because the named constant allows several blocks to be controlled
together. It also documents the reason by using a suitable name for the
constant.

--
ACCU Spring Conference 2003 April 2-5
The Conference you cannot afford to miss
Check the details: http://www.accuconference.co.uk/
Francis Glassborow      ACCU
--



Tue, 13 Sep 2005 14:18:47 GMT  
 Why many people will use #if 1 and #if 0 in C code?. What to do to compile that code?

Quote:

>Often, they are additions/removal where the developer is not quite sure
>if that is the right thing to do, except that it makes the program work
>correctly.

>From there, a second reason evolves: people forget about temporariness
>of the changes and never go back to clean up the source.

Similar to this is when the #if code is a sketch of either an alternative
implementation strategy or of a partially-implemented feature, etc. In
these cases it's effectively a delimiter for a comment that's written
in C rather than in English.

Soon after I added #if support to the old BSD unifdef program I had to
change it to not interpret constant #if expressions unless told to do
so, since in old code you frequently see #if 0 being used to delimit
this kind of comment. They are not the kind of thing that you want to
be removed as a side-effect of stripping out cryptographic extensions to
make a basic telnetd from a kerberized one (to pick a real-world example).

Tony.
--

SHANNON: NORTHERLY 4 OR 5. MAINLY FAIR. MAINLY GOOD.
--



Wed, 14 Sep 2005 16:00:21 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. Compile CS source code using ICodeCompiler

2. Why do people do this to their code?!

3. Why does this code even compile?

4. Why does this code not compile?

5. Why is this code fragment not compiling?

6. Why won't this code compile?

7. Why does not the code compile?

8. Why will MSDN code not compile?

9. Possibly why people post off-topic here...

10. Newbie: separate big .cs file into small .cs files

11. Code which used to compile in VC++ 6.0 gives C2593 error in VC++ .Net

12. Code which won't compile using Borland C Compiler

 

 
Powered by phpBB® Forum Software