What is the difference (if any) between the two following preprocessor control statements.
#if
and
#ifdef
You can demonstrate the difference by doing:
#define FOO 0
#if FOO
// won't compile this
#endif
#ifdef FOO
// will compile this
#endif
#if checks for the value of the symbol, while #ifdef checks the existence of the symbol (regardless of its value).
#ifdef FOO
is a shortcut for:
#if defined(FOO)
#if can also be used for other tests or for more complex preprocessor conditions.
#if defined(FOO) || defined(BAR)
Related
I'm working on some geometry-based code. The task at hand involves use of a bounding-box to contain the solid specimen. Now, in the code I have devised two different types of such boxes, namely INNER_BOUNDING_BOX and OUTER_BOUNDING_BOX. The code further expects use of any one of the two boxes, but not both. I'm trying to achieve it through the use of preprocessor.
I have written further code based on a couple of macros namely USE_INNER_BOUNDING_BOX and USE_OUTER_BOUNDING_BOX. I can ensure that at any time any one macro is defined through some simple construct like this:
#if defined(USE_INNER_BOUNDING_BOX) && defined(USE_OUTER_BOUNDING_BOX)
#undef USE_INNER_BOUNDING_BOX
#undef USE_OUTER_BOUNDING_BOX
#define USE_INNER_BOUNDING_BOX
#endif
#ifndef USE_INNER_BOUNDING_BOX
#ifndef USE_OUTER_BOUNDING_BOX
#define USE_INNER_BOUNDING_BOX
#endif
#endif
Now, if I wanted to use any particular box, I could just define the corresponding macro. The difficulty comes with wanting for use of a default setting macro say USE_DEFAULT_BOUNDING_BOX, which I could use to then set up define for any one of USE_INNER_BOUNDING_BOX or USE_OUTER_BOUNDING_BOX when both or none of them are explicitly defined.
I'd be inclined towards portable code, but compiler-specific trick could also pass. I'm using Visual Studio 2012.
Reliable one-of-many selections like those can better be done by selecting them with a single multi-value switch right away.
#define BOUNDING_INNER 1
#define BOUNDING_OUTER 2
/* default */
#define BOUNDING_BOX_TO_USE BOUNDING_INNER
/* alternatively please activate below line
#define BOUNDING_BOX_TO_USE BOUNDING_OUTER
*/
If you need to stay backward compatible to some configurations,
e.g. your code has already been used by others,
you can derive the single switch from the two, matching your default behaviour.
The advantage is to avoid #undef (in case you agree that it is an advantage to do so).
#if defined(USE_INNER_BOUNDING_BOX) && defined(USE_OUTER_BOUNDING_BOX)
#define BOUNDING_BOX_TO_USE BOUNDING_INNER
#endif
#ifndef USE_INNER_BOUNDING_BOX
#ifndef USE_OUTER_BOUNDING_BOX
#define BOUNDING_BOX_TO_USE BOUNDING_INNER
#endif
#endif
/* In case you are as paranoid a programmer as I am,
you might want to do some plausibility checking
here. ifndef, >0, <2 etc., triggering some #errors. */
/* Later, in code doing the actual implementation: */
#if (BOUNDING_BOX_TO_USE == BOUNDING_INNER)
/* do inner bounding stuff */
#endif
/* other code, e.g. common for inner and outer */
#if (BOUNDING_BOX_TO_USE == BOUNDING_OUTER)
/* do outer bounding stuff */
#endif
Since there are only two values I would use only one boolean variable:
#ifndef USE_OUTER_BOUNDING_BOX
#define USE_OUTER_BOUNDING_BOX 0
#endif
If USE_OUTER_BOUNDING_BOX is zero (false) the inner bounding box is used.
test.c:
#include <stdio.h>
#ifndef USE_OUTER_BOUNDING_BOX
#define USE_OUTER_BOUNDING_BOX 0
#endif
int main(void)
{
printf("%d\n", USE_OUTER_BOUNDING_BOX);
return 0;
}
Example:
$ cc -o test -DUSE_OUTER_BOUNDING_BOX=0 test.c
$ ./test
0
$ cc -o test -DUSE_OUTER_BOUNDING_BOX=1 test.c
$ ./test
1
Here is what I ended with:
// Comment to use bigger outer Bounding-Box
#define USE_INNER_BOUNDING_BOX
#define INNER_BOUNDING_BOX 0
#define OUTER_BOUNDING_BOX 1
#define DEFAULT_BOUNDING_BOX OUTER_BOUNDING_BOX
#if defined(USE_INNER_BOUNDING_BOX) && !defined(USE_OUTER_BOUNDING_BOX)
#define USE_BOUNDING_BOX INNER_BOUNDING_BOX
#elif defined(USE_OUTER_BOUNDING_BOX)
#define USE_BOUNDING_BOX OUTER_BOUNDING_BOX
#else
#define USE_BOUNDING_BOX DEFAULT_BOUNDING_BOX
#endif
#if (USE_BOUNDING_BOX == INNER_BOUNDING_BOX)
#undef USE_OUTER_BOUNDING_BOX
#define USE_INNER_BOUNDING_BOX
#else
#undef USE_INNER_BOUNDING_BOX
#define USE_OUTER_BOUNDING_BOX
#endif
This just works in this case. In case of more boxes, I'd append the conditional blocks.
I'm trying to use #ifndef as below.
#ifndef MACRO1 || #ifndef MACRO2
....
#endif
I already tried:
#ifndef (MACRO1 || MACRO2)
..
#endif
But for both cases am getting below error
error: extra tokens at end of #ifndef directive
Use the #if preprocessor directive instead:
#if !defined(MACRO1) || !defined(MACRO2)
#ifdef and #ifndef are special abbreviations for #if defined(...) and #if !defined(...). However, they can only be used for a single macro and do not allow for logical operations. So, if checking for multiple macros, use #if with the defined() operator instead. Being a regular operatior, this can be combined with logical operations, as the for !defined() already does.
You can use following code
#if !defined(MACRO1) || !defined(MACRO2)
#endif
You can use the defined operator in the #if directive to use
expressions that evaluate to 0 or 1 within a preprocessor line.
You can use logical operators in preprocessor directives, but in order to check something defined, use the defined directive:
#if !defined MACRO1 || !defined MACRO2
....
#endif
Is it possible to put a #endif inside a #if as the block's 'content' not as the pair #endif for the #if?
#if (SOME_CONDITION)
#if (ANOTHER_CONDITION)
#endif // pair endif for #if (SOME_CONDITION)
#if (SOME_CONDITION)
#endif // pair endif for #if (ANOTHER_CONDITION)
#endif // pair endif for #if (SOME_CONDITION)
If this is not possible how to conditionally compile a #if ... #endif pair?
This is what I was doing.
I was modifying a code base that we bought from another company. To compile it with and without my modifications easily I was using a macro say like shown below.
#if (MY_COMPANY_EDITS_ENABLED)
// My Modified code goes here
#else
// unmodified code from another company
#endif
In this way I could easily compile in/out my modifications while maintaining readability about my edits. I was using the same #if #else #endif blocks everywhere. But then I came across a code that is being compiled in, in the original unmodified code base, based on some macro value.
#if (FEATURE_A_IS_ENABLED)
// Line 1
// Line 2
#endif
But I want to compile this code [Line 1 and Line 2] regardless of the macro value FEATURE_A_IS_ENABLED
My first thought was to follow the same convention that I used till now [to maintain readability about my edits].
#if (MY_COMPANY_EDITS_ENABLED)
//#if (FEATURE_A_IS_ENABLED)
#else
#if (FEATURE_A_IS_ENABLED)
#endif
// Line 1
// Line 2
#if (MY_COMPANY_EDITS_ENABLED)
// #endif
#else
#endif
#endif
Then I realized this is not possible.
I know, alternative methods exist to achieve the same. But was wondering whether I could use the same convention
#if (MY_COMPANY_EDITS_ENABLED)
// My Modified code goes here
#else
// unmodified code from another company
#endif
in this case too.
No, it's not possible. The first #endif will be matched with the most recent #if or #else, so your code will be interpreted like this:
#if (SOME_CONDITION)
#if (ANOTHER_CONDITION)
#endif // pair endif for #if (ANOTHER_CONDITION)
#if (SOME_CONDITION)
#endif // pair endif for the second #if (SOME_CONDITION)
#endif // pair endif for the first #if (SOME_CONDITION)
This is not possible, as the preprocessor only does a single pass over your file, and the #endif gets matched with the preceding #if. If you want to make an #if/#endif block conditional, then just nest it inside another #if/#endif block:
#if CONDITION_A
# if CONDITION_B
...
# endif /* CONDITION_B */
#endif /* CONDITION_A */
Put another way, it's not possible to have preprocessing directives construct other preprocessing directives, as the output from the initial "construction" phase will not be reparsed by the preprocessor.
The (silly) example below wouldn't work either for example, even assuming the newlines wouldn't be an issue (which they would be here):
#if DEFINE_X_TO_FIVE
#define X
#endif
#if DEFINE_X_TO_FIVE
5
#endif
I recommend making the changes based on feature sets rather than whether or not they are yours or not then group them together to make a given version (MY_COMPANY_EDITS_ENABLED) or whatever:
#if (MY_COMPANY_EDITS_ENABLED)
#define FEATUREA
#define FEATUREB
#define FEATUREC
#define FEATURED
#else
#define FEATUREA
#undef FEATUREB
#undef FEATUREC
#undef FEATURED
#endif
#ifdef FEATUREA
//do some feature A stuff
#endif
// do code
#ifdef FEATUREB
//do some feature B stuff
#endif
#ifndef FEATUREC
//do some stuff if not feature C
#endif
// etc...
This is a lot more flexible in the long run and lets you switch changes features on and off with a rebuild.
Off course you can nest your preprocessor directives:
#ifdef CONDITION1
// some code here
# ifdef CONDITION2
// some else here
# endif
#endif
But make sure to end each condition properly.
See also: http://www.ioccc.org/years.html#1995_vanschnitz and http://www.ioccc.org/years.html#2004_vik2
Let us presume that we have the next fragment of code in a C program:
#ifdef USE_FORK
CODE...
#else
phtread_t thread;
pthread_create(&thread,NULL,clientDispatch,&client);
#endif
Can you explain me what are these directives, ifdef, else, endif. What happens when we use C directives?
Quoting cplusplus.com,
Preprocessor directives are lines included in the code of programs preceded by a hash sign (#). These lines are not program statements but directives for the preprocessor. The preprocessor examines the code before actual compilation of code begins and resolves all these directives before any code is actually generated by regular statements.
#ifdef allows a section of a program to be compiled only if the macro that is specified as the parameter has been defined, no matter which its value is. For example:
#ifdef TABLE_SIZE
int table[TABLE_SIZE];
#endif
In this case, the line of code int table[TABLE_SIZE]; is only compiled if TABLE_SIZE was previously defined with #define, independently of its value. If it was not defined, that line will not be included in the program compilation.
The #if, #else and #elif (i.e., "else if") directives serve to specify some condition to be met in order for the portion of code they surround to be compiled. The condition that follows #if or #elif can only evaluate constant expressions, including macro expressions. For example:
#if TABLE_SIZE > 200
#undef TABLE_SIZE
#define TABLE_SIZE 200
#elif TABLE_SIZE < 50
#undef TABLE_SIZE
#define TABLE_SIZE 50
#else
#undef TABLE_SIZE
#define TABLE_SIZE 100
#endif
int table[TABLE_SIZE];
Notice how the entire structure of #if, #elif and #else chained directives ends with #endif.
http://www.acm.uiuc.edu/webmonkeys/book/c_guide/1.7.html
This link should explain to you the directives. Additionally, with the directives if the condition after #ifdef becomes true then the compiler will compile the following code otherwise it would look for the next directive and compile the following code.
In your example therefore, if USE_FORK is a true expression then the CODE... will be compiled, otherwise, the lines with definition to threads will be compiled.
Its a conditional group, a preprocessor command. If the macro is defined i.e. USE_FORK then the code will executed up to #else, if the macro is not defined then the code at #else will execute up to #endif
I have a piece of code which I want to include if either of two macros are defined
#ifdef MACRO1 || MACRO2
void foo()
{
}
#endif
How do I accomplish this in C?
Besides #ifdef, the preprocessor supports the more general #if instruction; actually, #ifdef MACRO is a shortcut for #if defined(MACRO), where defined is a "preprocessor function" that returns 1 if the macro is defined; so, you can do:
#if defined(MACRO1) || defined(MACRO2)
void foo()
{
}
#endif
#if defined(MACRO1) || defined(MACRO2)
Here the NOT version if needed:
#if !defined(MACRO1) && !defined(MACRO2)
...
#endif
#if defined(Macro 1) + defined(Macro 2) == 1
<Code>
#endif