Lecture 19 CC Preprocessor - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Lecture 19 CC Preprocessor

Description:

Preprocessor replaces defined constants or macros with actual code ... Can be used to define almost any type of C constant (including structure classes, etc. ... – PowerPoint PPT presentation

Number of Views:65
Avg rating:3.0/5.0
Slides: 16
Provided by: jimand
Category:

less

Transcript and Presenter's Notes

Title: Lecture 19 CC Preprocessor


1
Lecture 19C/C Preprocessor
  • ENGR17 Engineering Programming
  • Section 1
  • Fall 2001

12/07/01
2
Outline
  • Components of the build process
  • Preprocessor
  • Constants
  • Conditional compilation
  • Header files
  • Parameterized macros

3
Compiling a Program
Text File Editor
Preprocessor
Build a Program
Compiler
Linker
Executable
4
Why a Preprocessor?
  • The first C compilers had no constants
  • Solution was to create a preprocessor that is run
    on the programs before they are passed to the C
    compiler
  • Preprocessor is nothing more than a specialized
    text editor
  • Preprocessor replaces defined constants or macros
    with actual code
  • Conceptually, preprocessor is not part of the
    C/C compiler
  • Preprocessor uses and entirely different systax
  • Practically, the preprocessor may be included
    with the main compiler

5
define Directive
  • General Form
  • define Name Substitute-Text
  • These two lines perform the same
    functiondefine SIZE 20const int SIZE 20
  • The preprocessor changes ALL instances of SIZE to
    the value 20 before sending the file to the
    compiler
  • ALL preprocessor commands begin with in column
    1
  • No semicolon at the end of a preprocessor line

6
define Example
Before Preprocessor
After Preprocessor
define SIZE 20 void main() int
arraySIZE int i for(i0iltSIZEi)
arrayi 10
void main() int array20 int i
for(i0ilt20i) arrayi 10
7
define versus const
  • const is relatively new, so most older code
    uses define directives
  • Using const is better
  • Syntax checked immediately
  • Uses C syntax instead of preprocessor syntax
  • Follows normal C scope rules
  • Can be used to define almost any type of C
    constant (including structure classes, etc.)

8
Conditional Compilation
  • Use the preprocessor to compile only certain
    sections of the code
  • Easier than commenting out lines of code
  • Debugging Code
  • Print messages to help with debug
  • When program is finished, turn off messages
  • Portable Code
  • C/C is not always portable
  • The same code may not work on different types of
    machines
  • UNIX and Windows

9
Debugging Example
  • define DEBUG / Turn on debugging /
  • undef DEBUG / Turn off debugging /
  • for (i0 ilt10 i)
  • DataFile gtgt valueifdef DEBUG cout ltlt
    Value ltlt i ltlt is
  • cout ltlt value ltlt endlendif

When DEBUG is defined, values will print to
console
10
Debugging Example
  • define DEBUG / Turn on debugging /
  • undef DEBUG / Turn off debugging /
  • ifdef DEBUG
  • cout ltlt Test Version Debug is on\n
  • else
  • cout ltlt Production Version\n
  • endif

11
Portability Example
  • define WIN32 / Windows application /
  • ifdef WIN32
  • Code for Windows OS
  • else
  • Code for UNIX OS
  • endif

12
include Directive
  • include is actually a preprocessor command
  • Examplesinclude ltiostream.hgtinclude
    rectangle.h
  • Tells the preprocessor to find the iostream.h
    file and insert it in the current program
  • ltgt indicates that the file is a standard header
    file
  • indicates that the file is use-defined (local)

13
Parameterized Macros
  • We can define macros that take parameters
  • Exampledefine SQR(x) ( (x) (x) )
  • This may simplify the code and make it easier to
    read
  • Would it be better to make the macro a function?

14
Macro Example
Before Preprocessor
After Preprocessor
define SQR(x)((x)(x)) void main() int
i4 i SQR(i) cout ltlt i ltlt endl
void main() int i4 i ii cout ltlt
i ltlt endl
15
Summary
  • Components of the build process
  • Preprocessor
  • Constants
  • Conditional compilation
  • Header files
  • Parameterized macros
Write a Comment
User Comments (0)
About PowerShow.com