The Objective-C Preprocessor is not part of the compiler, but is a separate step in the compilation process.
In simplistic terms, an Objective-C Preprocessor is just a text substitution tool and it instructs compiler to do required pre-processing before actual compilation.
All preprocessor commands begin with a pound symbol (#).
It must be the first nonblank character, and for readability
Preprocessor directive should begin in first column.
Lest of Preprocessor
- #define – Substitutes a preprocessor macro
- #include – Inserts a particular header from another file
- #undef -Undefines a preprocessor macro
- #ifdef -Returns true if this macro is defined
- #ifndef – Returns true if this macro is not defined
- #if – Tests if a compile time condition is true
- #else – The alternative for #if
- #elif -#else an #if in one statement
- #endif – Ends preprocessor conditional
- #error -Prints error message on stderr
- #pragma -Issues special commands to the compiler using a standardized method
#define MAX_ARRAY_LENGTH 20
This directive tells the OCPP to replace instances of MAX_ARRAY_LENGTH with 20. Use #define for constants to increase readability.
These directives tell the OCPP to get foundation.h from Foundation Framework
#define FILE_SIZE 42
This tells the OCPP to undefine existing FILE_SIZE and define it as 42.
#define MESSAGE “You wish!”
This tells the OCPP to define MESSAGE only if MESSAGE isn’t already defined.
Your debugging statements here
This tells the OCPP to do the process the statements enclosed if DEBUG is defined. This is useful if you pass the -DDEBUG flag to gcc compiler at the time of compilation. This will define DEBUG, so you can turn debugging on and off on the fly during compilation.
ANSI C defines a number of macros. Although each one is available for your use in programming, the predefined macros should not be directly modified.
The Objective-C preprocessor offers following operators to help you in creating macros:
- Macro Continuation (\)
- Stringize (#)
- Token Pasting (##)
- The defined() Operator
Macro Continuation (\)
A macro usually must be contained on a single line.
The macro continuation operator is used to continue a macro that is too long for a single line.
#define message_for(a, b) \ NSLog(@#a " and " #b ": We love you!\n")
The stringize or number-sign operator (‘#’), when used within a macro definition, converts a macro parameter into a string constant. This operator may be used only in a macro that has a specified argument or parameter list.
Token Pasting (##)
The token-pasting operator (##) within a macro definition combines two arguments. It permits two separate tokens in the macro definition to be joined into a single token.
The defined() Operator
The preprocessor defined operator is used in constant expressions to determine if an identifier is defined using #define. If the specified identifier is defined, the value is true (non-zero). If the symbol is not defined, the value is false (zero). The defined operator is specified as follows:
One of the powerful functions of the OCPP is the ability to simulate functions using parameterized macros. For example, we might have some code to square a number as follows
int square(int x)
return x * x;
We can rewrite above code using a macro as follows:
#define square(x) ((x) * (x))
Macros with arguments must be defined using the #define directive before they can be used. The argument list is enclosed in parentheses and must immediately follow the macro name. Spaces are not allowed between macro name and open parenthesis