Difference between revisions of "The C Preprocessor and pound Defines"

From OpenCircuits
Jump to navigation Jump to search
Line 49: Line 49:
  
  
 +
External links to preprocessor information:
  
  
 +
* [http://gcc.gnu.org/onlinedocs/gcc-3.0.1/cpp_3.html#SEC33  3. Macros]
  
 +
* [http://gcc.gnu.org/onlinedocs/cpp/ The C Preprocessor]
  
External links to preprocessor information:
+
* [http://publications.gbdirect.co.uk/c_book/chapter7/directives.html 7.3. Directives]

Revision as of 18:14, 19 May 2009

Early draft, may not be useful to read

Overview

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.

Calculations

    #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;

becomes

      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


Calculations

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.

More Reading

Other places in this wiki where the preprocessor is discussed:

boostc h files

Index of sample code pages


External links to preprocessor information: