SM Ryan wrote:[color=blue]
> tweak <xbwaichunasx@c ox.net> wrote:
> #
> # What does this do?
> #
> # #if 1
> # include "x.h"
> # #endif
> #
> # I have never seen #if with just a 1.
>
> It does absolutely nothing.
>
> I use them on some cases where I have temporary code I need to turn off
> and on, and it's easiest to just keep editting #if 1 to #if 0 to #if 1,
> etc.
>
> In case you haven't seen that
> #if 0
> ...
> #endif
> unconditionally blocks ... from the being passed to the compiler.
>
> --
> SM Ryan http://www.rawbw.com/~wyrmwif/
> OOOOOOOOOO! NAVY SEALS![/color]
Thanks. Sorry about the typo in the include, should read #include "x.h".
So it's just a good way to edit code. Never thought about using
#if for that. Learn something new every day.
"tweak" <xbwaichunasx@c ox.net> wrote in message news:t5JEc.5576 $151.2394@fed1r ead02...[color=blue]
>
> What does this do?
>
> #if 1
> include "x.h"
> #endif
>
> I have never seen #if with just a 1.
>
> Thanks,
>
> Brian[/color]
Nested comments are not allowed in C. So, if you want to comment
the comment(s), you use #if 0 ... #endif combination. For example,
12: ....
15: /* some comments */
17: ....
You want to comment everything between line 12 and 17, which you
simply can not do with another /* and */ pair, because there is already
one comment present in the line 15. So, you do it the followin way:
#if 0
12: ....
15: /* some comments */
17: ....
#endif
On the other hand, some compilers like Turbo C/C++ provides nesting of
comments as an extension.
In 'comp.lang.c', SM Ryan <wyrmwif@tang o-sierra-oscar-foxtrot-tango.fake.org>
wrote:
[color=blue]
> # Thanks. Sorry about the typo in the include, should read #include "x.h".
>
> Realized that.
>
> # So it's just a good way to edit code. Never thought about using
> # #if for that. Learn something new every day.
>
> I won't claim it's _good_ way, but it is a way.[/color]
It's a Good Way to me (with some /* 'recall' */ on #endif). It allows code
with comments to be ignored. It's very useful to test a new version of a
piece of code, and to come back quickly to the previous version if needed.
On Wed, 30 Jun 2004, tweak wrote:
[color=blue]
>
> What does this do?
>
> #if 1
> include "x.h"
> #endif
>
> I have never seen #if with just a 1.[/color]
It allows the programmer to enable/disable a block of code. When I first
started programming C, if there was a section of 'debug' code I would do
something like:
/*
debug code goes here
*/
when I wanted to create a final release build. I soon found that some
compilers didn't like nested comments. So the following would fail to
compile:
/*
/* this is just debug code */
debug code goes here
*/
The easy solution was to use the #ifdef 1 preprocessor. So while I wanted
to see the debug information I would use:
#ifdef 1
/* this is just debug code */
debug code goes here
#endif
when it came time to compile the release code I would change this line to:
#ifdef 0
/* this is just debug code */
debug code goes here
#endif
Later I just learned to use proper macros and my code would read as:
#ifdef ENABLE_MY_DEBUG _CODE
/* this is just debug code */
debug code goes here
#endif
then when I compiled I'd just make sure the macro ENABLE_MY_DEBUG _CODE was
set to 1.
Comment