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

From OpenCircuits
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
Early draft, may not be useful to read
 
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 )
 
== Overview ==  
 
== Overview ==  
  
Line 72: Line 72:
  
 
* [http://publications.gbdirect.co.uk/c_book/chapter7/directives.html 7.3. Directives]
 
* [http://publications.gbdirect.co.uk/c_book/chapter7/directives.html 7.3. Directives]
 +
 +
* [http://www.gamedev.net/reference/articles/article1798.asp Organizing Code Files in C and C++] Not just the preprocessor but more general information on the structure of projects.
  
  
 
[[Category:PIC]][[Category:BoostC]]
 
[[Category:PIC]][[Category:BoostC]]

Latest revision as of 10:43, 3 October 2009

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 )

Overview[edit]

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[edit]

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[edit]

    #warning yyyy

sends warning yyyy to the output and continues the compilation process ( actually in the preprocessor )

    #error xxxxxx

sends error xxxxxx to the output and ends compilation process

How C Compiles[edit]

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[edit]

Other places in this wiki where the preprocessor is discussed:

boostc h files

Index of sample code pages


External links to preprocessor information: