Simple Preprocessor Question

T

tweak

What does this do?

#if 1
include "x.h"
#endif

I have never seen #if with just a 1.

Thanks,

Brian
 
S

SM Ryan

#
# 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.
 
T

tweak

SM said:
#
# 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.

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.

Thanks Again,

Brian
 
V

Vijay Kumar R Zanvar

tweak said:
What does this do?

#if 1
include "x.h"
#endif

I have never seen #if with just a 1.

Thanks,

Brian

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.
 
S

SM Ryan

# 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.
 
E

Emmanuel Delahaye

# 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.

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.
 
D

Darrell Grainger

What does this do?

#if 1
include "x.h"
#endif

I have never seen #if with just a 1.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

preprocessor bug? 2
Preprocessor question 3
Preprocessor limitation workarounds 32
preprocessor trick 4
Preprocessor commands usage. 11
A simple form question 2
preprocessor 2
Linux: using "clone3" and "waitid" 0

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top