The Preprocessor - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

The Preprocessor

Description:

All preprocessor directives begin with the #' sign ... #define JEFF 'Rox the World' Preprocessor replaces all occurrences of JEFF with 'Rox the World' ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 15
Provided by: Chas97
Category:

less

Transcript and Presenter's Notes

Title: The Preprocessor


1
The Preprocessor
2
The Preprocessor
  • Preprocessing occurs before compilation
  • Things that occur
  • inclusion of other files
  • defining constants and macros
  • conditional compilation
  • conditional execution of preprocessor directives

3
Preprocessor (cont)
  • All preprocessor directives begin with the
    sign
  • Only whitespace characters may appear before this
  • They do not end with a semicolon

4
include
  • Causes a copy of code to be included
  • Two forms
  • include ltsomething.hgt - something.h is found
    where standard libraries are found.
  • include other.h - other.h can be found in
    current directory (of file being compiled) or
    where standard libs are found. Used when
    defining your own header files (.h files)

5
define
  • Used for defining constants
  • Constant variables are IN CAPITAL LETTERS
  • Example
  • define JEFF Rox the World
  • Preprocessor replaces all occurrences of JEFF
    with Rox the World
  • define everything you can!!!!

6
Macros
  • Tiny little functions
  • Used commonly in C (C uses inline functions)
  • No data type checking in macros
  • Example
  • define SQUARE ( x ) ( x x )
  • myNum SQUARE ( 9 ) // before replacement
  • myNum ( 9 9 ) // after replacement

7
Problem with Macros
  • define SQUARE ( x ) ( x x )
  • void main ( )
  • int myNum 0
  • int someNum 5
  • myNum SQUARE ( someNum 7)
  • // Whats wrong here?

myNum someNum 7 someNum 7
8
Solution
  • Use more parentheses
  • define SQUARE ( x ) ( ( x ) ( x ) )
  • Replacement is now
  • myNum (someNum 7) (someNum 7)

9
undef
  • Used to un-define a constant or macro
  • Why would you want to do this?
  • Be careful if you undefine a constant or macro
    from another library!

10
ifdef, ifndef, endif
  • Allows programmer to control how the code is
    compiled
  • Used to prevent repeatedly defining and including
    things (when you have multiple files)
  • Used with endif

11
Example
  • include ltiostream.hgt
  • ifndef JEFF
  • define JEFF Rox the World
  • endif

12
Another Example
  • include ltiostream.hgt
  • define DEBUG 1
  • // Define the DEBUG constant to be TRUE
  • void main (void)
  • if (DEBUG)
  • cout ltlt DEBUG is WORKING! ltlt endl
  • else
  • cout ltlt DEBUG is not working ltlt endl

13
Note about inline functionsThis isnt the
preprocessor stuff
  • Inline functions are similar to macros
  • Speeds up code (no function call)
  • Text replacement (making executable bigger)
  • inline functions can be ignored by compiler - but
    not macros
  • inlines have type checking (but not macros)
  • Example
  • inline int square (int x) return x x

14
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com