Axe
Axe
Axe
Recall that preprocessing is the first step in the C program compilation stage -- this
feature is unique to C compilers.
The preprocessor more or less provides its own language which can be a very
powerful tool to the programmer. Recall that all preprocessor directives or commands
begin with a #.
The preprocessor also lets us customise the language. For example to replace { ... }
block statements delimiters by PASCAL like begin ... end we can do:
#define begin {
#define end }
#define
Use this to define constants or any macro substitution. Use as follows:
For Example
#define FALSE 0
#define TRUE !FALSE
We can also define small ``functions'' using #define. For example max. of two
variables:
All it means that wherever we place max(C ,D ) the text gets replaced by the
x = max(q+r,s+t);
after preprocessing, if we were able to look at the code it would appear like
this:
#undef
This commands undefined a macro. A macro must be undefined before being
redefined to a different value.
#include
This directive includes a file into code.
It has two possible forms:
#include <file>
or
#include ``file''
<file> tells the compiler to look where system include files are held.
``file'' looks for a file in the current directory (where program was run
from)
We can have else etc. as well by using #else and #elif -- else if.
These are useful for checking if macros are set -- perhaps from different program
modules and header files.
For example, to set integer size for a portable C program between TurboC (on
MSDOS) and Unix (or other) Operating systems. Recall that TurboC uses 16
bits/integer and UNIX 32 bits/integer.
Assume that if TurboC is running a macro TURBOC will be defined. So we just need to
check for this:
#ifdef TURBOC
#define INT_SIZE 16
#else
#define INT_SIZE 32
#endif
#define LINELENGTH 80
The setting of such flags is useful, especially for debugging. You can put commands
like:
#ifdef DEBUG
print("Debugging: Program Version 1\");
#else
print("Program Version 1 (Production)\");
#endif
Also since preprocessor command can be written anywhere in a C program you can
filter out variables etc for printing etc. when debugging:
x = y *3;
#ifdef DEBUG
print("Debugging: Variables (x,y) = \",x,y);
#endif
The -E command line is worth mentioning just for academic reasons. It is not that
practical a command. The -E command will force the compiler to stop after the
preprocessing stage and output the current state of your program. Apart from being
debugging aid for preprocessor commands and also as a useful initial learning tool
(try this option out with some of the examples above) it is not that commonly used.