Chapter 10 Preprocessor - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 10 Preprocessor

Description:

Chapter 10 Preprocessor – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 21
Provided by: radf7
Category:

less

Transcript and Presenter's Notes

Title: Chapter 10 Preprocessor


1
Chapter 10 Preprocessor
  • By C. Shing
  • ITEC Dept
  • Radford University

2
Objectives
  • Understand predefined system macro
  • Understand how to use macro with arguments

3
Macro
  • Called in-line function
  • Has separate name space (either upper
  • or lower case) from function names
  • More efficient than function for short
  • function code

4
Macro (Cont.)
  • Types of macro
  • include
  • define, undef
  • Coditional macro if, ifdef, ifndef
  • error
  • line linenumber
  • Operators ,

5
Macro (Cont.)
  • Example
  • define getchar() getc(stdin)
  • define putchar(c) putc((c), stdout)
  • define swap(xptr,yptr) int tmp tmpxptr \
  • xptryptr yptrtmp

6
Predefined System Macro
  • Begins and end with two underscores
  • __DATE__ current date
  • __TIME__ current time
  • __FILE__ current file name
  • __LINE__ current line number

7
Define Macro with Argument
  • Do not leave space between macro and argument
  • Do not put at the end of the macro definition
  • Treat argument as an entity by putting () around
  • argument
  • Treat the whole result as an entity by putting ()
  • around argument

8
Define Macro with Argument (Cont.)
  • Example
  • define CUBE(x) ((x)(x)(x))
  • if (i3)
  • xCUBE(y2)
  • else
  • xy2
  • The above code is expanded intp
  • If (i3)
  • x((y2)(y2)(y2))
  • else
  • xy2

9
Cancel Macro Definition
  • Cancel macro definition before redefine it
  • Form undef (macroname)
  • Example
  • undef CUBE

10
Conditional Macro
  • Form
  • if
  • statements
  • endif
  • Or
  • if
  • statements
  • elif
  • statements
  • else
  • statements
  • endif

11
Conditional Macro (Cont.)
  • Example
  • if (y gt -2)
  • xCUBE(y2)
  • else
  • xy2
  • endif

12
Conditional Macro (Cont.)
  • Form (Cont.)
  • ifdef
  • ifndef or if !defined()

13
Conditional Macro (Cont.)
  • Example
  • ifndef CUBE
  • define CUBE(x) ((x)(x)(x))
  • endif
  • Or
  • if !defined(CUBE)
  • define CUBE(x) ((x)(x)(x))
  • endif

14
Conditional Macro (Cont.)
  • Class Example
  • Create a platform independent code
  • Example1
  • DOS Executable

15
Error Macro
  • Form error error message
  • Example
  • if (ylt-2)
  • error Not allowed to calculate (y2)3
  • endif

16
Line Macro
  • Form line linenumber filename.c
  • Starts renumber using the linenumber to
    filename.c
  • Example
  • line 1000 renumbered_file.c

17
Implementation Dependent Macro
  • Form pragma tokens
  • If token is in implentation, take action.
  • Otherwise, ignore it.

18
Operators ,
  • format replaced by string
  • Example
  • define integer_addition(x, y) \
  • printf(x y d,xy)
  • inetger_addition(3,4) // print out 347

19
Operators , (Cont.)
  • merge tokens
  • Example
  • define Y(i) yi
  • Y(1)Y(2)Y(3)Y(4)100
  • // this produces y1y2y3y4100

20
References
  • Deitel Deitel C How to Program, 4th ed.,
  • Chapter 13, Prentice Hall
Write a Comment
User Comments (0)
About PowerShow.com