#ifndef Compile a section of code if an identifier is undefined ------------------------------------------------------------------------------ Syntax #ifndef <identifier> <statements>... [#else] <statements>... #endif Arguments <identifier> is the name of a definition whose absence is being verified. Description #ifndef...#endif lets you perform conditional compilation by identifying a section of source code to compile if the specified <identifier> is undefined. The #else directive specifies the code to compile if <identifier> is defined. The #endif terminates the conditional compilation block. Examples . This code fragment is a general skeleton for conditional compilation with #ifndef: #define DEBUG . . <statements> . #ifndef DEBUG <optimized version of code> #else <debugging version of code> #endif . This example compiles a section of code if a specific identifier is undefined. In DOS: C>CLIPPER Myfile In the program (.prg) file: #ifndef NODEBUG Assert(<some condition>) #endif . This example overrides a default definition in the program (.prg) file using a manifest constant defined on the compiler command line with the /D option In DOS: C>CLIPPER Myfile /DM_MARGIN=10 In the program (.prg) file: #ifndef M_MARGIN #define M_MARGIN 15 #endif
Tag Archives: #ifndef
C5 Directives
#command Specify a user-defined command or translation directive #define Define a manifest constant or pseudofunction #error Generate a compiler error and display a message #ifdef Compile a section of code if an identifier is defined #ifndef Compile a section of code if an identifier is undefined #include Include a file into the current source file #stdout Send literal text to the standard output device #translate Specify a user-defined command or translation directive #undef Remove a #define macro definition #xcommand Specify a user-defined command or translation directive #xtranslate Specify a user-defined command or translation directive
C5 Pre-processor Directives
#command | #translate :
Specify a user-defined command or translation directive
#command <matchPattern> => <resultPattern> #translate <matchPattern> => <resultPattern>
#define :
Define a manifest constant or pseudofunction
#define <idConstant> [<resultText>] #define <idFunction>([<arg list>]) [<exp>]
#error :
Generate a compiler error and display a message
#error [<messageText>]
#ifdef :
Compile a section of code if an identifier is defined
#ifdef <identifier> <statements>... [#else] <statements>... #endif
#ifndef :
Compile a section of code if an identifier is undefined
#ifndef <identifier> <statements>... [#else] <statements>... #endif
#include :
Include a file into the current source file
#include "<otherSourceFileSpec>"
#stdout :
Send literal text to the standard output device
#stdout [<messageText>]
#undef :
Remove a #define macro definition
#undef <identifier>
#xcommand | #xtranslate :
Specify a user-defined command or translation directive
#xcommand <matchPattern> => <resultPattern> #xtranslate <matchPattern> => <resultPattern>
Conditional compilation
What are conditional compilation directives ?
They are : #ifdef, #ifndef, #else and #endif.
#ifdef
Compile a section of code if an identifier is defined.
Syntax :
#ifdef <identifier> <statements>... [#else] <statements>... #endif
Arguments :
<identifier> is the name of a definition whose existence is being verified.
Description :
#ifdef…#endif lets you perform a conditional compilation. It does this by identifying a section of source code to be compiled if the specified <identifier> is defined. The <identifier> can be defined using either the #define directive or the /D compiler option which lets you define an identifier or manifest constant from the compiler command line.
The #else directive specifies the code to compile if <identifier> is undefined. The #endif terminates the conditional compilation block.
Conditional compilation is particularly useful when maintaining many different versions of the same program. For example, the demo code and full system code could be included in the same program file and controlled by a single #define statement.
Examples :
. This code fragment is a general skeleton for conditional compilation with #ifdef:
#define DEMO . . <statements> . #ifdef DEMO <demo specific statements> #endif
. This example controls conditional compilation with an identifier defined on the compiler command line with the /D option.
In the program (.prg) file:
#ifdef DEBUG Assert(<some condition>) #endif
. This example defines a manifest constant to one value if it does not exist and redefines it to another if it exists:
#ifdef M_MARGIN #undef M_MARGIN #define M_MARGIN 15 #else #define M_MARGIN 10 #endif
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#ifndef
Compile a section of code if an identifier is undefined
Syntax :
#ifndef <identifier> <statements>... [#else] <statements>... #endif
Arguments :
<identifier> is the name of a definition whose absence is being verified.
Description :
#ifndef…#endif lets you perform conditional compilation by identifying a section of source code to compile if the specified <identifier> is undefined.
The #else directive specifies the code to compile if <identifier> is defined. The #endif terminates the conditional compilation block.
Examples :
. This code fragment is a general skeleton for conditional compilation with #ifndef:
#define DEBUG . . <statements> . #ifndef DEBUG <optimized version of code> #else <debugging version of code> #endif
. This example compiles a section of code if a specific identifier is undefined.
In the program (.prg) file:
#ifndef NODEBUG Assert(<some condition>) #endif
. This example overrides a default definition in the program (.prg) file using a manifest constant defined on the compiler command line with the /D option
In the program (.prg) file:
#ifndef M_MARGIN #define M_MARGIN 15 #endif
Pre-processor
What is Pre-processor and how it works ?
The Pre-processor is a translation program that prepares source code for compilation by applying pre-defined text replacements. The replacements to be made are specified by directives in the source file. The preprocessor operates transparently as a part of the compilation process.
First phase of of compilation process is pre-processing. The portion of compile engine that perform pre-processing phase is called pre-processor.
Before the later compilation phases take place, pre-processor scan the source file from top to bottom for certain directives and translate them into regular source code that can be compiled. The output of the pre-processor is then used as input to the next step of compilation. The /P compiler option can be used to write this pre-processor output (.ppo) file to disk so that you can see the source code that was used as input to the next compilation phase. This option is especially useful if you have used the #command and #translate directives to build user-defined commands. You can look at the (.ppo) file to see whether or not commands translated as you expected.
The following table summarize the pre-processor directives.
Directive Meaning -------------------- -------------------------------------------------------------- #define Define a manifest constant or pseudo-function #undef Remove a #define definition #include Include a file into the current source file #command, #xcommand, Specify a user defined command or translation #translate, directive (This four directives are same as each #xtranslate others with some minor differences) . #ifdef Compile a section of code if an identifier is defined #ifndef Compile a section of code if an identifier is undefined #error Generate a compiler error and display a message #stdout Send literal text to the standard output device
Here you can find pre-processor terms.