The C Preprocessor and pound Defines
Early draft, may not be useful to read Or: The C Preprocssor and #Defines ( #Define seems to be illegal in the title of a page )
Programming in C almost always involves using and learning about the pre processor. The preprocessore is a program that takes your program as input and outputs the program somewhat transformed.
The #define may be the most important part of the preprocessor. A #define simply defines one symbol as a shorthand for another symbol, all the data is text or string. For example
#define BIG 255
Simply means that every place that the string BIG occurs in the program it means the string 255. The preprocessor actually makes this substution. There is no notion that 255 is a number, it is just a string, later the compiler will see the 255 and it, not the preprocessor will view it as a number.
#define BIGGER 255 + 5
just looks like a calculation, actually it means that BIGGER is just the same as the text 250 + 5, only later in the compiler will 255 + 5 be interperted as a number where:
x = a + BIGGER;
x = a + 255 + 5;
now you might think that this code will make the runtime code add 255 and 5 at runtime, however most optimizing compilers will perform the calculation at compile time
The ability to do calculations with the preprocessor is a bit odd, as shown above most of the #define work simply does text substution, with the effect of calculation comming in the compiler optimization. There is an odd way of doing calculations, the #if directive will evalue what follows it interperting the symbols as long integers, and treating 0 as false and anything else as true. You can do the calculation, and even compare the result with ==, but there does not seem to be any direct way of saving the result. Here is an example:
Look at the links below for more information about #if and related directives.
Messages in the Output
sends warning yyyy to the output and continues the compilation process ( actually in the preprocessor )
sends error xxxxxx to the output and ends compilation process
How C Compiles
In particurlar how BoostC compiles. This is sort of right, what I think I have figured out so far.
- Files are saved
- Preprocessor runs and transforms the file
- Compiler converts file to assembler
- Assembly code is assembled to machine code object files
- Linker mereges object files and resolves references between them, also performns some optimizations like deleting subroutines that are not called.
Other places in this wiki where the preprocessor is discussed:
boostc h files
External links to preprocessor information:
- Organizing Code Files in C and C++ Not just the preprocessor but more general information on the structure of projects.